webpack报错:Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Cannot find module 'babel-preset-env' from '...' - Did you mean "@babel/env"?

  • webpack报错:Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Cannot find module 'babel-preset-env' from '...' - Did you mean "@babel/env"?
    原因:
    babel-core和babel-preset-env依赖被替换为@babel/core和@babel/preset-env,因为新版本的Babel7中对官方提供的依赖使用了命名空间@babel
    npm install babel-loader babel-core babel-preset-env --save-dev
    npm install babel-loader @babel/core @babel/preset-env --save-dev
    解决:
    在.babelrc文件或在webpack.config.js文件中,将原来的env替换为@babel/preset-env
// .babelrc

{
  "presets": [
    ["env",
      {
        "modules": false
      }
    ]
  ]
}
// webpack.config.js

module: {
    rules: [
        {
            test: /\.js$/,
            exclude: /(node_modules)/,
            use: {
                loader: 'babel-loader',
                options: {
                    presets: ['env', { modules: false }]
                }
            }
        }
    ]
}
  • babel官方:https://babeljs.io/docs/en/babel-preset-env#browserslist-integration
点赞