模块构建失败无法找到从 babel\index.js 导入的包 \@babel\core\

module build failed cannot find package \@babel\core\ imported from babel\index.js

这个问题我已经纠结好几天了。我做了一些研究,但在 vain.After“ng serve”终端中总是报告:

> ./node_modules/@angular/router/fesm2020/router.mjs - Error: Module build failed (from ./node_modules/@angular-devkit/build-angular/src/babel/webpack-loader.js):
Error [ERR_MODULE_NOT_FOUND]: Cannot find package '\node_modules\@angular\compiler-cli\node_modules\@babel\core\' imported from \node_modules\@angular\compiler-cli\bundles\linker\babel\index.js
Did you mean to import @babel/core/lib/index.js?
    at legacyMainResolve (internal/modules/esm/resolve.js:225:9)
    at packageResolve (internal/modules/esm/resolve.js:648:14)
    at moduleResolve (internal/modules/esm/resolve.js:696:18)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:810:11)
    at Loader.resolve (internal/modules/esm/loader.js:88:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:241:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:56:40)
    at link (internal/modules/esm/module_job.js:55:36)

我的 package.json 如下所示:

"devDependencies": {
"@angular-devkit/build-angular": "^13.0.2",
"@angular/cli": "^13.0.2",
"@angular/compiler-cli": "~13.0.1",
"@babel/cli": "^7.16.8",
"@babel/core": "^7.16.7",
"@babel/node": "^7.16.8",
"@babel/preset-env": "^7.16.8",
"@types/file-saver": "^2.0.3",
"@types/jasmine": "~3.8.2",
"@types/node": "^16.7.10",
"@types/xlsx": "0.0.36",
"babel-loader": "^8.2.3",
"jasmine-core": "~3.8.0",
"karma": "~6.3.4",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.0.3",
"karma-jasmine": "~4.0.1",
"karma-jasmine-html-reporter": "~1.7.0",
"typescript": "~4.4.4",
"webpack": "^5.66.0"}

我的 .babelrc 是:

{"presets": ["@babel/preset-env"]}

你能给我一些建议吗?谢谢!

我遇到了同样的问题。 node_modules 中一定有一些奇怪的缓存在进行。我不知道发生了什么,但我能够通过 运行 ..

解决

rm -rf node_modules

然后

npm install

希望对你有用!