javascript 模块构建失败:错误:找不到模块“@babel/core”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48693654/
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
Module build failed: Error: Cannot find module '@babel/core'
提问by Giesburts
So I am sort of new to webpack since I never created my own workflow and I am trying to set up an enviorement to compile ES2015+ JS and SCSS code now for the first time. For some reason when I try to compile my webpack I get the error Module build failed: Error: Cannot find module '@babel/core'. Yet I have installed it with npm install
所以我对 webpack 有点陌生,因为我从来没有创建过自己的工作流,我现在第一次尝试建立一个环境来编译 ES2015+ JS 和 SCSS 代码。出于某种原因,当我尝试编译我的 webpack 时出现错误Module build failed: Error: Cannot find module '@babel/core'。然而我已经安装了它npm install
Webpack.config.js
webpack.config.js
module.exports = {
entry: './components/js/app.js',
output: {
filename: './dist/main.js'
},
module: {
loaders: [
{test: /\.js$/, loader: "babel-loader", exclude: /node_modules/},
{test: /\.scss$/, loader: "style-loader!css-loader!sass-loader"}
],
}
}
.babelrc
.babelrc
{
"presets": [
"@babel/preset-env"
]
}
Package.json
包.json
{
"name": "webpack-env",
"version": "1.0.0",
"description": "Webpack",
"main": "components/js/app.js",
"scripts": {
"build": "webpack"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/preset-env": "^7.0.0-beta.39",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"css-loader": "^0.28.5",
"node-sass": "^4.5.3",
"sass-loader": "^6.0.6",
"style-loader": "^0.18.2",
"webpack": "^3.4.1"
},
"dependencies": {}
}
Is there something I am missing?
有什么我想念的吗?
回答by Giesburts
Alright after some digging I've found the problem. The problem is that I've installed a newer version of babel, @babel/preset-envwhich is babel version 7. Version 6 isn't working that way (as I believe, not sure) and for version 6 you have to run npm install --save-dev babel-preset-envinstead of npm install --save-dev @babel/preset-envas I did.
好吧,经过一番挖掘,我发现了问题所在。问题是我安装了较新版本的 babel,@babel/preset-env即 babel 版本 7。版本 6 不能那样工作(我相信,不确定)并且对于版本 6,您必须运行npm install --save-dev babel-preset-env而不是npm install --save-dev @babel/preset-env像我那样运行。
The .babelrc file becomes
.babelrc 文件变成
{
"presets": ["env"]
}
回答by anubhab
This solved the issue for me for babel 7.
这为我解决了 babel 7 的问题。
Inside .babelrc file
在 .babelrc 文件中
{
"presets": [
[
"@babel/preset-env",
{
"modules": "commonjs",
"targets": {
"node": "current"
}
}
]
]
}
https://github.com/babel/babel-loader/issues/616#issuecomment-386107677
https://github.com/babel/babel-loader/issues/616#issuecomment-386107677
Edit: This also works.
编辑:这也有效。
{
"presets": ["@babel/preset-env"]
}
{
"presets": ["@babel/preset-env"]
}
回答by Ricky Nyairo
In order to use Babel 7, I believe your .babelrc file should have the following or similar:
为了使用 Babel 7,我相信你的 .babelrc 文件应该有以下或类似的内容:
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}

