node.jsjestjsbabeljs

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


Just cli suddently has stopped working, all test fail 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"]
    }
  }
}

Solution

  • 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 below should fix your problem:

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