- 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