Sarah
Sarah

Reputation: 160

Persistent Jest Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3"

I am getting a persistent Jest error whenever I use npm to install my dependencies on the server side. Using yarn to install the same dependencies works, but I am currently working on a team where we're all using npm. I have tried all of the proposed solutions on Stack Overflow, upvoted or not, and none have worked for me. The two senior devs I've asked so far don't think there is anything in my globally installed npm packages that would cause this.

I get this error for every Jest test suite I run:

● Test suite failed to run

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)

This is what my package.json looks like:

{
  "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"

} }

Here is my .babelrc file:

{
  "presets": [
   "@babel/preset-env"
  ],
  "plugins": [
    [
      "@babel/plugin-proposal-decorators",
      {
        "decoratorsBeforeExport": true
      }
    ],
    "@babel/plugin-proposal-class-properties"
  ]
}

Upvotes: 0

Views: 432

Answers (2)

jackdbd
jackdbd

Reputation: 5053

As George Artemiou says, this error occurs because you are using Babel 7 in your project, while jest still uses Babel 6.

I had the same issue, and I solved it by installing babel-core@^7.0.0-bridge.0

See also: https://github.com/babel/babel-bridge

Upvotes: 1

George Artemiou
George Artemiou

Reputation: 3166

The problem is with jest dependencies. I had the same issue and resolved it by adding the step below to my package.json file

"scripts": {
  ...
  "postinstall": "rimraf node_modules/jest-runtime/node_modules/babel-core node_modules/jest-config/node_modules/babel-core",
  ...  
}

Hope this helps..

Upvotes: 0

Related Questions