Mattias
Mattias

Reputation: 285

Jest: Couldn't find preset "@babel/env" relative to directory

I'm trying to set up Jest in an ES6 project. Didn't get far, since as soon as I run jest, I start running into problems.

.babelrc

{
  "presets": [
    "@babel/env"
  ]
}

The above throws a Couldn't find preset "@babel/env" relative to directory error when running Jest. Which is fine I guess, since it should be @babel/preset-env either way? (both work with the rest of my setup though...)

But! When I change the preset to @babel/preset-env, the error I'm receiving is very confusing:

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: "/Users/.../node_modules/@babel/preset-env/lib/index.js")

Which is very strange, since my devDeps of Babel and Jest are set to:

"devDependencies": {
  "@babel/core": "^7.2.0",
  "@babel/preset-env": "^7.2.0",
  "@babel/register": "^7.0.0",
  "babel-jest": "^23.6.0"
}

As far as I can tell, that error message makes exactly zero sense. My next thought was to check my global Babel CLI installation, which was [email protected]. Closer, but it's not 6.26.3 as stated in the error message. I also tried replacing the global package with the latest @babel/cli, but it made no difference.

What might I be missing here?

Upvotes: 3

Views: 1739

Answers (1)

Mattias
Mattias

Reputation: 285

Ah, solved it. For some very strange reason, I had to include 'babel-core@^7.0.0-0' in my local setup, which I found out at the babel-jest npmjs page (screenshot below):

2]

So basically, running npm i -D 'babel-core@^7.0.0-0' solved my problem, leaving my devDeps like this:

"devDependencies": {
  "@babel/core": "^7.2.2",
  "@babel/preset-env": "^7.2.0",
  "@babel/register": "^7.0.0",
  "babel-core": "^7.0.0-bridge.0",
  "babel-jest": "^23.6.0",
}

Including both @babel/core and babel-core seems like madness to me, but hey, it works! The 6.26.3 error message remains a mystery I guess, since I can't find any Babel package at all at that version, neither local or global...

Upvotes: 3

Related Questions