救命!Requires Babel "^7.0.0-0", but was loaded with "6.26.3".

想通过babel-node让后端node可以运行es6,结果装完babel-cli总是报错Requires Babel "^7.0.0-0", but was loaded with "6.26.3".具体如下:

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.
    at throwVersionError (/Users/apple/HBuilderXProjects/travel-house/node_modules/@babel/helper-plugin-utils/lib/index.js:65:11)
    at Object.assertVersion (/Users/apple/HBuilderXProjects/travel-house/node_modules/@babel/helper-plugin-utils/lib/index.js:13:11)
    at _default (/Users/apple/HBuilderXProjects/travel-house/node_modules/@babel/plugin-proposal-decorators/lib/index.js:43:7)
    at /Users/apple/HBuilderXProjects/travel-house/node_modules/@babel/helper-plugin-utils/lib/index.js:19:12
    at Function.memoisePluginContainer (/Users/apple/HBuilderXProjects/travel-house/node_modules/babel-register/node_modules/babel-core/lib/transformation/file/options/option-manager.js:113:13)
    at Function.normalisePlugin (/Users/apple/HBuilderXProjects/travel-house/node_modules/babel-register/node_modules/babel-core/lib/transformation/file/options/option-manager.js:146:32)
    at /Users/apple/HBuilderXProjects/travel-house/node_modules/babel-register/node_modules/babel-core/lib/transformation/file/options/option-manager.js:184:30
    at Array.map (<anonymous>)
    at Function.normalisePlugins (/Users/apple/HBuilderXProjects/travel-house/node_modules/babel-register/node_modules/babel-core/lib/transformation/file/options/option-manager.js:158:20)
    at OptionManager.mergeOptions (/Users/apple/HBuilderXProjects/travel-house/node_modules/babel-register/node_modules/babel-core/lib/transformation/file/options/option-manager.js:234:36)
    at OptionManager.init (/Users/apple/HBuilderXProjects/travel-house/node_modules/babel-register/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at compile (/Users/apple/HBuilderXProjects/travel-house/node_modules/babel-register/lib/node.js:103:45)
    at loader (/Users/apple/HBuilderXProjects/travel-house/node_modules/babel-register/lib/node.js:144:14)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/apple/HBuilderXProjects/travel-house/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
    at Object.<anonymous> (/Users/apple/HBuilderXProjects/travel-house/node_modules/babel-cli/lib/_babel-node.js:154:22)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)

我的package.json配置如下

 "dependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/helper-plugin-utils": "^7.0.0",
    "@babel/runtime": "^7.2.0",
    "@svgr/webpack": "2.4.1",
    "antd": "^3.12.1",
    "babel-cli": "^6.26.0",
    "babel-eslint": "9.0.0",
    "babel-jest": "^23.6.0",
    "babel-loader": "8.0.4",
    "babel-plugin-import": "^1.8.0",
    "babel-plugin-named-asset-import": "^0.3.0",
    "babel-preset-react-app": "^7.0.0",
    "bfj": "6.1.1",
    ...
  },
"devDependencies": {
    "@babel/core": "^7.2.2",
    "@babel/plugin-proposal-decorators": "^7.2.3",
    "@babel/preset-env": "^7.2.3",
    "babel-core": "^7.0.0-bridge.0",
    "babel-plugin-import": "^1.11.0",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-decorators-legacy": "^1.3.5",
    "jest": "^23.6.0"
  }

我已经折腾了好几个小时,网上能试的方法都尝试,依然是这个报错!求大神们救命!

阅读 20.9k
5 个回答

把 node_modules 文件夹下的都删了,在重新 npm install 一下

另外,"babel-cli": "^6.26.0" 这行是不是冲突了

要看具体的版本号,可以去 package-lock.json, 或者如果你用了yarn,那就去 yarn-lock.json 中找。

采纳的答案确实说了解决方案,但很模糊,我也遇到了这个问题,花了大半天才搞定,我来解释一下。
不过题主当时为了解决问题,引入了一堆冲突,这种情况首先应该移除所有 babel 相关依赖。
安装 babel-cli 默认安装依赖 babel/core 6.26.3,基本可以说这种不带 @ 的语法将停留在 v6 版本里。
所以要用 v7 的话,就应该使用 @babel/cli,提供 babel 命令行使用环境,这个版本并不会自动安装 @babel/core,而是需要自行安装匹配依赖,所以要能执行 babel,再加上使得 node 能运行 JavaScript 新特性,最小安装为:

yarn add @babel/cli @babel/core @babel/node @babel/preset-env -D

你的依赖中既有babel-cli,也有@babel/cli,既有babel-cor也有@babel/core,可能会引起冲突吧,最好确认下,安装一个就可以了。

新手上路,请多包涵
  1. npm i -g @babel/core @babel/node

运行babel-node通过以下方式

npx babel-node youself.js
新手上路,请多包涵

我的解决 重装下 babel-loader 之前版本是7.X 重装后8.X 可用

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏