node.js 找不到模块“@babel/core”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52067944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:53:41  来源:igfitidea点击:

Cannot find module '@babel/core'

node.jsreactjsnpmwebpackbabeljs

提问by J. Doe

I am following along with this webpack4/react tutorial:

我正在关注这个 webpack4/react 教程:

https://www.youtube.com/watch?v=deyxI-6C2u4

https://www.youtube.com/watch?v=deyxI-6C2u4

I have followed it exactly up until the part where he runs npm start. The difference is, his app runs, and mine gets the error:

我一直遵循它,直到他运行 npm start 的部分。不同之处在于,他的应用程序运行,而我的应用程序出现错误:

Cannot find module '@babel/core'

找不到模块“@babel/core”

The full error:

完整的错误:

ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module '@babel/core'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\joeyf\Desktop\Code\Github\webpack4-sample\node_modules\babel-loader\lib\index.js:5:15)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
 @ multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./src/index.js main[2]

I have tried to reinstall babel-core but is still not being found. Here is my package.json:

我尝试重新安装 babel-core 但仍然没有找到。这是我的 package.json:

{
  "name": "webpack4-sample",
  "version": "1.0.0",
  "description": "A sample setup of Webpack4 with React and Babel",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --mode development --open --hot",
    "build": "webpack --mode production"
  },
  "author": "Joey Fenny",
  "license": "ISC",
  "dependencies": {
    "babel": "^6.23.0",
    "babel-cli": "^6.26.0",
    "react": "^16.4.2",
    "react-dom": "^16.4.2"
  },
  "devDependencies": {
    "babel-core": "^7.0.0-rc.4",
    "babel-loader": "^8.0.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "html-webpack-plugin": "^3.2.0",
    "webpack": "^4.17.1",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.6"
  }
}

My webpack.config.js:

我的 webpack.config.js:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: './src/index.js',
    output: {
        path: path.join(__dirname, '/dist'),
        filename: 'index_bundle.js'
    },
    module: {
        rules: [{
            test: /\.js$/,
            exclude: path.join(__dirname, '/node_modules'),
            use: {
                loader: 'babel-loader'
            }
        }]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html'
        })
    ]
}

Here is a link to a git repo:

这是一个 git repo 的链接:

https://gitlab.com/jfny/webpack4-sample

https://gitlab.com/jfny/webpack4-sample

Anyone know whats going on? Thanks.

有谁知道怎么回事?谢谢。

回答by Wesgur

Try running this.

尝试运行这个。

npm install @babel/core --save

babel changed their package so your babel-corewill not be the same as @babel/core.

babel 改变了他们的包,所以你babel-core不会和@babel/core.

回答by Robin Wieruch

The recent Babel upgrade to version 7 changed the namings of the node packages.

最近 Babel 升级到版本 7 更改了节点包的命名。

For instance, now you have to install

例如,现在你必须安装

npm install --save-dev @babel/core @babel/preset-env

and

npm install --save-dev @babel/preset-react

to get it working with React. Then you can use these in your .babelrcfile:

让它与 React 一起工作。然后你可以在你的.babelrc文件中使用这些:

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

Or as alternative, when not having a .babelrc, in your package.json:

或者,当您的package.json 中没有.babelrc 时

...
"keywords": [],
"author": "",
"license": "ISC",
"babel": {
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ]
},
"devDependencies": {
...

If you want to get more into it, you can checkout this recent Webpack + Babel + React setup.

如果你想深入了解,你可以查看最近的Webpack + Babel + React 设置

回答by Mandakini

I am able to fix this issue using below command

我可以使用以下命令解决此问题

npm install @babel/core --save

回答by MinteLiu.l

babel-loader@8 requires Babel 7.x (the package '@babel/core'). If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'.

babel-loader@8 需要 Babel 7.x('@babel/core' 包)。如果你想使用 Babel 6.x ('babel-core'),你应该安装 'babel-loader@7'。

回答by Atta Ur Rehman

In my case I had to uninstall babel 6

就我而言,我不得不卸载 babel 6

npm uninstall --save-dev babel-cli  babel-core babel-polyfill babel-preset-es2015 babel-preset-stage-2 babel-register

and then reinstall babel 7

然后重新安装 babel 7

npm i  --save-dev  @babel/cli  @babel/core @babel/node  @babel/polyfill  @babel/preset-env

and it worked for me.

它对我有用。

回答by pouya

for those of you who use @babel/core alongside babel-node: i just installed @babel/core using npm i @babel/core --save-devbut when i tried to use babel-node it does not recognized the @babel/core package, i uninstalled @babel/core and installed it again using npm i @babel/core --saveand it worked again!

对于那些在 babel-node 旁边使用 @babel/core 的人:我刚刚安装了 @babel/core usingnpm i @babel/core --save-dev但是当我尝试使用 babel-node 时它无法识别 @babel/core 包,我卸载了 @babel/core 和使用再次安装它,它再次npm i @babel/core --save工作!

回答by Mohammed_Alreai

try to install npm install @babel/core --save if it does not work try to uninstall node_modules and reinstall

尝试安装 npm install @babel/core --save 如果它不起作用尝试卸载 node_modules 并重新安装

回答by akhil

I solved the same error by removing all the babel modules from dev Dependenciesexecuting the below command:

我通过执行以下命令从dev Dependencies 中删除所有 babel 模块解决了同样的错误:

npm install -D babel-loader @babel/core @babel/preset-env

npm install -D babel-loader @babel/core @babel/preset-env

You can refer to this link if the above command does not work:

如果上述命令不起作用,您可以参考此链接:

[https://github.com/babel/babel/issues/8599#issuecomment-417866691]

[ https://github.com/babel/babel/issues/8599#issuecomment-417866691]

回答by Sakil Mahmud

I removed the existing npm uninstall babel-core babel-preset-env babel-preset-react

我删除了现有的 npm uninstall babel-core babel-preset-env babel-preset-react

and added their new names npm install --save-dev @babel/core @babel/preset-env @babel/preset-reactthat's works for me as perfectly fine.

并添加了他们的新名字 npm install --save-dev @babel/core @babel/preset-env @babel/preset-react,这对我来说非常好。

回答by rld

I fixed with:

我修复了:

 npm install --save-dev babel-loader@7 babel-core babel-preset-env webpack webpack-cli -D