10

我实际上没有对代码进行任何更改,突然有一天我醒来发现了这个错误,

backend: internal/modules/cjs/loader.js:454
backend:   const e = new Error(`Package exports for '${basePath}' do not define a ` +
backend:             ^
backend: Error: Package exports for '/Users/kumarabhirup/Documents/Repositories/propagateAt/node_modules/tslib' do not define a valid '.' target
backend:     at resolveExportsTarget (internal/modules/cjs/loader.js:454:13)
backend:     at resolveExports (internal/modules/cjs/loader.js:387:16)
backend:     at Function.Module._findPath (internal/modules/cjs/loader.js:486:20)
backend:     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:781:27)
backend:     at Function.Module._load (internal/modules/cjs/loader.js:687:27)
backend:     at Module.require (internal/modules/cjs/loader.js:849:19)
backend:     at require (internal/modules/cjs/helpers.js:74:18)
backend:     at Object.<anonymous> (/Users/kumarabhirup/Documents/Repositories/propagateAt/node_modules/apollo-link/lib/index.js:3:15)
backend:     at Module._compile (internal/modules/cjs/loader.js:956:30)
backend:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10) {
backend:   code: 'MODULE_NOT_FOUND'
backend: }
backend: [nodemon] app crashed - waiting for file changes before starting...

这个来自另一个 StackOverflow 问题的人更改了他们的节点版本,并且对他们有用,我也尝试过(v12.xx -> v15.xx),对我不起作用。

我还升级typescript,尝试安装tslib,做yarn cache clean,删除并重新安装 node_modules,删除yarn.lock,没有任何效果。

请帮忙。

这是我的package.json

{
  "name": "backend",
  "version": "1.0.0",
  "license": "ISC",
  "main": "dist/src/index.js",
  "scripts": {
    "build": "tsc -p tsconfig.release.json",
    "build:watch": "tsc --watch -p tsconfig.release.json",
    "start": "npm run build && node --experimental-modules --experimental-json-modules --es-module-specifier-resolution=node dist/src/index.js",
    "start:pm2": "NODE_ENV=production pm2 start dist/src/index.js --node-args=\"--max_old_space_size=500 --experimental-modules --experimental-json-modules --es-module-specifier-resolution=node\"",
    "dev": "npm run build && concurrently --raw \"nodemon --experimental-modules --experimental-json-modules --es-module-specifier-resolution=node dist/src/index.js\" \"npm run build:watch\"",
    "lint": "eslint . --ext js,jsx,ts,tsx",
    "lint:fix": "eslint . --fix --ext js,jsx,ts,tsx",
    "test": "NODE_ENV=test jest --passWithNoTests --watch",
    "test:ci": "NODE_ENV=test jest --passWithNoTests"
  },
  "devDependencies": {
    "@types/bull": "^3.14.0",
    "@types/mongoose": "^5.5.43",
    "@types/node": "^12.7.3",
    "@types/node-cron": "^2.0.2",
    "@types/nodemailer": "^6.4.0",
    "concurrently": "^5.1.0",
    "eslint": "^6.8.0",
    "eslint-config-airbnb-typescript-prettier": "^2.1.0",
    "nodemon": "^1.19.1",
    "pm2": "^4.4.0",
    "prettier": "^1.18.2",
    "tslib": "^2.0.3",
    "typescript": "^3.7.0",
    "typings": "^2.1.1"
  },
  "dependencies": {
    ...
    "tslib": "^2.0.3",
    ...
  },
  "engines": {
    "node": ">=12.x"
  },
  "resolutions": {
    "graphql": "14.x"
  }
}

这是我的tsconfig.json

{
  "compilerOptions": {
    "target": "es2019",
    "module": "commonjs",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "allowJs": true,
    "importHelpers": true,
    "alwaysStrict": true,
    "sourceMap": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    // "lib": ["es2017"],
    "types": ["jest"]
  },
  "include": ["src/**/*", "__tests__/**/*"],
  "paths": {
    "tslib" : ["node_modules/tslib/tslib.d.ts"]
  },
}

这是我的tsconfig.release.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "rootDir": ".",
    "outDir": "dist",
    "removeComments": true
  },
  "include": ["src/**/*"]
}

谢谢!

4

4 回答 4

15

我遇到了类似的问题,我通过降级节点版本(从 v13.0.1 到 v12.19.0)解决了问题。这可能不是理想的解决方案,但它可以帮助您在寻找最终解决方案的同时解除阻碍。

我使用nvm for windows来管理节点版本。

我给你一个问题和解决方案的截图。

我希望它有助于解决您的问题。

问候!

于 2020-10-27T12:11:28.387 回答
5

对我来说,这些步骤起到了作用:

  1. 降级到最新的NodeJS稳定版本:nvm install --lts
  2. 删除/node_modulespackage-lock.json文件
  3. 杀死 pm2 进程并再次添加:pm2 killpm2 start process.json

诀窍实际上是杀死 pm2 并再次添加该进程。祝你好运!

于 2020-11-02T20:10:07.340 回答
0

如果对我来说,这解决了:

  1. 降级到最新版本的 Node v12。
  2. 删除 node_modules 文件夹,然后重新安装它们。

注意: 我使用的是 Angular v8,所以我查看了他们的更新站点,以查看在升级到 Angular 8 时推荐的 Node 版本,它是 Node v10,并且由于 Node 应该向后兼容,我刚刚从 Node v13 降到 Node v12 .

于 2021-06-28T11:43:03.263 回答
0

我遇到了同样的问题,并且跑了npm audit fix四次,有时带有--force标志并且在此处的其他答案都不起作用之后为我修复了它。

于 2022-01-05T17:13:57.157 回答