简体   繁体   中英

Module build failed: Error: Cannot find module '@babel/core'

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.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

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

Package.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?

Alright after some digging I've found the problem. The problem is that I've installed a newer version of babel, @babel/preset-env which 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-env instead of npm install --save-dev @babel/preset-env as I did.

The .babelrc file becomes

{
  "presets": ["env"]
}

In order to use Babel 7, I believe your .babelrc file should have the following or similar:

{
  "presets": [
    [
      "@babel/preset-env", 
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM