每当我使用npm在服务器端安装我的依赖项时,我都会收到一个持久的Jest错误。使用yarn安装相同的依赖项是可行的,但是我目前正在一个团队中使用npm。我已经尝试过所有关于Stack Overflow的建议解决方案,无论是否支持,都没有一个适合我。到目前为止,我问过的两位资深开发人员认为在我全球安装的npm软件包中没有任何会导致这种情况的原因。
我运行的每个Jest测试套件都会出现此错误:
●测试套件无法运行
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 tolook for the first entry that doesn't mention "@babel/core" or "babel-core" to see what is calling Babel.
at throwVersionError (node_modules/@babel/helper-plugin-utils/lib/index.js:65:11)
at Object.assertVersion (node_modules/@babel/helper-plugin-utils/lib/index.js:13:11)
at _default (node_modules/@babel/plugin-proposal-decorators/lib/index.js:35:7)
at node_modules/@babel/helper-plugin-utils/lib/index.js:19:12
at Function.memoisePluginContainer (../../../node_modules/babel-core/lib/transformation/file/options/option-manager.js:113:13) at Function.normalisePlugin (../../../node_modules/babel-core/lib/transformation/file/options/option-manager.js:146:32) at ../../../node_modules/babel-core/lib/transformation/file/options/option-manager.js:184:30
at Array.map (<anonymous>)
at Function.normalisePlugins (../../../node_modules/babel-core/lib/transformation/file/options/option-manager.js:158:20)
at OptionManager.mergeOptions (../../../node_modules/babel-core/lib/transformation/file/options/option-manager.js:234:36)
at OptionManager.init (../../../node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
at File.initOptions (../../../node_modules/babel-core/lib/transformation/file/index.js:212:65)
at new File (../../../node_modules/babel-core/lib/transformation/file/index.js:135:24)
at Pipeline.transform (../../../node_modules/babel-core/lib/transformation/pipeline.js:46:16)
这是我的package.json的样子:
{
"name": "nanny-now",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest --verbose --runInBand",
"test:watch": "npm run test -- --watch",
"build": "babel src -d lib -s true",
"start": "node lib/index.js",
"start:watch": "nodemon src/index.js --exec babel-node"
},
"jest": {
"testEnvironment": "node"
},
"repository": {
"type": "git",
"url": ""
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/cli": "^7.1.5",
"@babel/core": "^7.2.0",
"@babel/node": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-decorators": "^7.1.6",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/preset-env": "^7.1.6",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.6.0",
"chance": "^1.0.18",
"eslint": "^5.9.0",
"eslint-plugin-babel": "^5.3.0",
"jest": "^23.6.0",
"nodemon": "^1.18.7",
"supertest": "^3.3.0"
},
"dependencies": {
"bcryptjs": "^2.4.3",
"dotenv": "^6.2.0",
"express": "^4.16.4",
"jsonwebtoken": "^8.4.0",
"mongoose": "^5.3.14",
"morgan": "^1.9.1",
"regenerator-runtime": "^0.13.1"
} }
这是我的.babelrc文件:
{
"presets": [
"@babel/preset-env"
],
"plugins": [
[
"@babel/plugin-proposal-decorators",
{
"decoratorsBeforeExport": true
}
],
"@babel/plugin-proposal-class-properties"
]
}
答案 0 :(得分:0)
问题在于玩笑依赖性。我遇到了同样的问题,并通过将以下步骤添加到package.json文件中来解决了该问题
"scripts": {
...
"postinstall": "rimraf node_modules/jest-runtime/node_modules/babel-core node_modules/jest-config/node_modules/babel-core",
...
}
希望这会有所帮助。
答案 1 :(得分:0)
正如George Artemiou所说的,发生此错误的原因是您在项目中使用Babel 7,而Jest仍在使用Babel 6。
我遇到了同样的问题,我通过安装babel-core@^7.0.0-bridge.0