问题描述
我实际上没有对代码进行任何更改,突然有一天我醒来了这个错误,
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...
This person from another StackOverflow question更改了它们的节点版本,并且对他们有用,我也尝试过(v12.x.x-> v15.x.x),对我不起作用。
我还升级了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,"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/**/*"]
}
谢谢!
解决方法
我遇到了类似的问题,并且通过降级节点版本(从v13.0.1降到v12.19.0)来解决。这可能不是理想的解决方案,但它可以帮助您在寻找最终解决方案时解除局面。
我使用 nvm for windows 管理节点版本。
我为您提供问题和解决方案的屏幕截图。
我希望这有助于您解决问题。
致谢!
,对我来说,这些步骤可以解决问题:
- 降级为最新的
NodeJS
稳定版本:nvm install --lts
- 删除
/node_modules
和package-lock.json
文件 - 杀死pm2进程并再次添加:
pm2 kill
和pm2 start process.json
诀窍实际上是杀死pm2并再次添加该过程。 祝你好运!
,这对我来说解决了:
- 已降级到最新版本的 Node v12。
- 删除 node_modules 文件夹,然后重新安装。
注意: 我使用的是 Angular v8,所以我检查了他们的 update site 以查看升级到 Angular 8 时推荐的 Node 版本,它是 Node v10,并且因为 Node 应该向后兼容,所以我刚刚从 Node v13 降级到 Node v12.