需要 Babel "7.0.0-0" 但加载了 "6.26.3"

IT技术 reactjs react-native babeljs
2021-04-16 05:11:18

在我遵循其他类似报告时,无论我尝试安装什么(babel wise),都会不断收到此错误。这是堆栈跟踪:

error: bundling failed: 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. (While processing preset:
"C:\\Users\\Admin-ESS\\Absent\\node_modules\\@babel\\preset-env\\lib\\index.js")
    at throwVersionError (C:\Users\Admin-ESS\Absent\node_modules\@babel\preset-env\node_modules\@babel\helper-plugin-utils\lib\index.js:65:11)
    at Object.assertVersion (C:\Users\Admin-ESS\Absent\node_modules\@babel\preset-env\node_modules\@babel\helper-plugin-utils\lib\index.js:13:11)
    at _default (C:\Users\Admin-ESS\Absent\node_modules\@babel\preset-env\lib\index.js:150:7)
    at C:\Users\Admin-ESS\Absent\node_modules\@babel\preset-env\node_modules\@babel\helper-plugin-utils\lib\index.js:19:12
    at C:\Users\Admin-ESS\Absent\node_modules\metro\node_modules\babel-core\lib\transformation\file\options\option-manager.js:317:46
    at Array.map (<anonymous>)
    at OptionManager.resolvePresets (C:\Users\Admin-ESS\Absent\node_modules\metro\node_modules\babel-core\lib\transformation\file\options\option-manager.js:275:20)
    at OptionManager.mergePresets (C:\Users\Admin-ESS\Absent\node_modules\metro\node_modules\babel-core\lib\transformation\file\options\option-manager.js:264:10)
    at OptionManager.mergeOptions (C:\Users\Admin-ESS\Absent\node_modules\metro\node_modules\babel-core\lib\transformation\file\options\option-manager.js:249:14)
    at OptionManager.init (C:\Users\Admin-ESS\Absent\node_modules\metro\node_modules\babel-core\lib\transformation\file\options\option-manager.js:368:12)
    at File.initOptions (C:\Users\Admin-ESS\Absent\node_modules\metro\node_modules\babel-core\lib\transformation\file\index.js:212:65)
    at new File (C:\Users\Admin-ESS\Absent\node_modules\metro\node_modules\babel-core\lib\transformation\file\index.js:135:24)
    at Pipeline.transform (C:\Users\Admin-ESS\Absent\node_modules\metro\node_modules\babel-core\lib\transformation\pipeline.js:46:16)
    at Object.transform (C:\Users\Admin-ESS\Absent\node_modules\metro\src\transformer.js:135:5)
    at Object.transformCode [as transform] (C:\Users\Admin-ESS\Absent\node_modules\metro\src\JSTransformer\worker\index.js:253:15)
    at execMethod (C:\Users\Admin-ESS\Absent\node_modules\jest-worker\build\child.js:92:29)
    at process.on (C:\Users\Admin-ESS\Absent\node_modules\jest-worker\build\child.js:42:7)
    at process.emit (events.js:180:13)
    at emit (internal/child_process.js:783:12)

和我的 package.json:

{
  "name": "Absent",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "babel-preset-react-native-stage-0": "^1.0.1",
    "jest": "^23.5.0",
    "jest-react-native": "^18.0.0",
    "react-test-renderer": "16.3.1"
  },
  "scripts": {
    "start": "react-native start",
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "test": "jest"
  },
  "jest": {
    "preset": "react-native"
  },
  "dependencies": {
    "@babel/core": "^7.0.0-rc.1",
    "@babel/preset-env": "^7.0.0-rc.1",
    "@babel/preset-react": "^7.0.0-rc.1",
    "jail-monkey": "^1.0.0",
    "prop-types": "^15.6.2",
    "react": "16.3.1",
    "react-native": "^0.55.4",
    "react-native-device-info": "^0.22.4",
    "react-native-elements": "^0.19.1",
    "react-native-firebase": "^4.3.8",
    "react-native-modal": "^6.5.0",
    "react-native-router-flux": "^4.0.1",
    "react-native-size-matters": "^0.1.2",
    "react-native-vector-icons": "^5.0.0",
    "react-redux": "^5.0.7",
    "redux": "^4.0.0",
    "redux-thunk": "^2.3.0"
  },
  "rnpm": {
    "assets": [
      "./assets/fonts/"
    ]
  }
}

任何指针将不胜感激在这里...

6个回答

用cmd测试你运行的是哪个版本

babel -V

如果不是 7 或更高版本

npm uninstall babel-cli -g
npm uninstall babel-core -g

npm install @babel/cli -g
npm install @babel/core -g

如果您使用 Jest 运行

npm install babel-core@7.0.0-bridge.0 --save-dev

如果你做节点开发,卸载并重新安装@babel/node 可以解决这个问题。

也解决了我的问题。谢谢!
2021-05-23 05:11:18
谢谢!这解决了我的问题。我还需要在我的存储库中本地执行安装和卸载步骤!
2021-06-08 05:11:18
卸载并重新安装 @babel/node 对我有用,谢谢
2021-06-11 05:11:18

看起来您需要babel-core按照文档的建议进行安装https : //jestjs.io/docs/en/getting-started#using-babel

yarn add --dev babel-jest babel-core@^7.0.0-bridge.0 @babel/core regenerator-runtime

尝试使用 babel-core@^7.0.0-bridge.0 并且成功了。感谢您的及时回复:)
2021-05-22 05:11:18
可爱的解决方案。
2021-05-26 05:11:18
这个问题对我来说没有解决:(
2021-06-02 05:11:18

有时是因为你同时安装了babel-cli 和 babel/cli,或者babel-core 和 @babel/core 导致冲突

所以

1) 删除 node_modules

2) 从 package.json 中删除 babel-cli、babel-core,保留 @babel/core、@babel/cli

3) 安装

babel-cli@babel/cli冲突

bable-core@babel/core冲突

我在网上找到的解决方案都没有奏效,我通过以下方式修复了它:

  1. 从 package.json 中删除jest@babel/core(保留babel-corev7 桥接器)
  2. 删除 node_modules 和 package-lock.json
  3. 安装
  4. npm install jest @babel/core --save-dev

对于那些仍在抗争的人,4 天前 Jest v24 发布,原生支持 babel 7。享受。

谢谢!。对我来说,这意味着从我的文件中删除require('babel-register')require('@babel/polyfill/noConflict')globalSetup
2021-06-02 05:11:18
升级到 Jest v24 对我有用,不需要babel-core@^7.0.0-bridge.0(实际上没有用)。两者都试过了,我觉得这应该是 2019 年公认的答案。
2021-06-16 05:11:18