【webpack】WebPack 引入 babel 报错 Cannot find module '@babel/core'

报错信息

ERROR in ./main.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
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'.
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (E:\Demo\vue\demo5-webpack\node_modules\babel-loader\l
ib\index.js:10:11)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
i 「wdm」: Failed to compile.

报错原因

babel 的版本冲突。
Babel - 7.X 对应的是 babel - loader 8.X
Babel - 6.X 对应的是 babel - loader 7.X

检查当前项目的 pageage.json 。发现果然版本出现了问题:
err

问题解决

  • 需要注意的一个问题是:babel 的命名
    babel/core 在 7.X 版本的时候命名发生了变化。

所以,使用 npm i -D babel-core 安装最新版 babel-core 时,默认安装时的 babel-core 6.26.3,也就是 babel-core 6.X 的最新版本。
如果需要安装 babel 7.X 的话,需要使用 npm i -D @babel/core 命令。将默认安装 @babel/core 7.X 的最新版本。

切记这个命名的变更(@符号 、 横线变为斜线)

npm i -D @babel/core
npm WARN demo5-webpack@1.0.0 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules\fse
vents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@
1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"ia32"
})

同样发生命名变化的还有 @babel/cli 等,都需要注意

npm i -D @babel/cli
npm WARN demo5-webpack@1.0.0 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules\fse
vents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@
1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"ia32"
})

+ @babel/cli@7.4.4
added 5 packages from 4 contributors and audited 11483 packages in 23.047s
found 0 vulnerabilities

安装完新版之后记得顺手卸载旧版本的 babel-core 等。
因为命名不同,新版并不会覆盖旧版

npm uninstall babel-core
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HolaSecurity

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值