具体报错如下:
Error: Cannot find module ‘@babel/core’
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’.
在git bush中使用该命令下载两个开发依赖项:npm install babel-core babel-loader -D
此时在 package.json 中可见 “devDependencies” 下两个依赖项的版本分别为 @6.26.3 和 @8.0.0
“babel-core”: “^6.26.3”,
“babel-loader”: “^8.0.0”,
编译错误原因:
由于babel-loader被更新到版本8之后,就不支持原来的配置了。
官方默认babel-loader | babel 对应的版本需要一致: 即babel-loader需要搭配最新版本babel
即需要如下对应:(来自官方:babel-loader)
webpack 4.x | babel-loader 8.x | babel 7.x
npm install babel-loader @babel/core @babel/preset-env webpack
webpack 4.x | babel-loader 7.x | babel 6.x
npm install babel-loader@7 babel-core babel-preset-env webpack
解决方案:
回退低版本
npm install -D babel-loader@7 babel-core babel-preset-env
更新到最高版本:
npm install -D babel-loader @babel/core @babel/preset-env webpack