在安装babel到 v7.0.0之后,使用babel命令偶尔会出现如下错误:
Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a compatible version of @babel/core, it is likely that something in your build process is loading the wrong version. Inspect the stack trace of this error to look for the first entry that doesn't mention "@babel/core" or "babel-core" to see what is calling Babel.
原因自然是因为babel核心和babel配置版本不匹配造成的。
而我自己因为以前全局安装过babel6,从而导致这个错误。
因此我先是全局卸载babel6,然后根据官网,更新babel核心 @babel/core @babel/cli 到 @^7.0.0 ,其余插件按需安装。
以下为搜索解决方案时查到的其余解决办法:
如果有其他插件强制使用了babel6,那么更新核心之后可能仍然无法解决问题,此时可以安装 babel-core@^7.0.0-bridge.0 解决。
npm i babel-core@7.0.0-bridge.0 -D
使用这种方法就不一定需要更新 @babel/cli 了