Cannot find module 'babel-preset-env' from PATH Did you mean "@babel/env"?

Viewed 33873

Jest cli suddently has stopped working, all test fails with:

Cannot find module 'babel-preset-env' from '/PATH'
    - Did you mean "@babel/env"?

I'm sure it is some kind of incompatibility between a module which uses babel 6 and my project with babel 7, but I'm not sure how can I solve.

This is package.json

{
  "name": "testing101",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "parcel index.html",
    "test": "jest src/*.js --watchAll",
    "nyc": "nyc ava",
    "build": "babel src/*.js -d dist"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.4.5",
    "@babel/preset-env": "^7.4.5",
    "@types/jest": "^24.0.13",
    "jest": "^24.8.0",
    "nyc": "^14.1.1",
    "parcel-bundler": "^1.7.0"
  },
  "jest": {
    "verbose": true,
    "testURL": "http://localhost:1234/",
    "collectCoverage": true
  },
  "dependencies": {
    "axios": "^0.18.0",
    "nock": "^10.0.6"
  }
}

This is .babelrc

{
  "presets": ["@babel/preset-env"],
  "env": {
    "test": {
      "presets": ["env"]
    }
  }
}
4 Answers

In the test section of the .babelrc file you have accidentally used an unscoped preset ("presets": ["env"]). This used to be the way to do it before Babel 7 and the @babel scope. More than that, since you are already configuring @babel/preset-env as a global preset, you don't need to define it again for the test environment. Removing the entire test environment configuration as bellow should fix your problem:

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

you can try this via npm

npm install --save-dev @babel/preset-env

This worked for me:

.babelrc

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

.eslintrc (add @babel/core to the existing list of configs)

{
  "extends": ["@babel/core", ...EXISTING-CONFIGS-HERE]
}

I met this problem when I opened 2 terminal with 2 different versions of node. ( I installed NVM to use different versions of nodes )

  • terminal 1: npm install ( correct, this is node 14 )
  • terminal 2: npm run dev ( error, this is node 16 )

so the solution is quite simple:

  1. remove the node_modules
  2. run npm install and npm run dev with the same node version.
Related