【问题标题】:Cannot find module 'babel-preset-env' from PATH Did you mean "@babel/env"?无法从 PATH 中找到模块 'babel-preset-env' 你的意思是“@babel/env”吗?
【发布时间】:2019-10-24 10:15:14
【问题描述】:

Jest cli 突然停止工作,所有测试都失败了:

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

我确定使用 babel 6 的模块与使用 babel 7 的项目之间存在某种不兼容,但我不确定如何解决。

这是 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"
  }
}

这是.babelrc

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

【问题讨论】:

    标签: node.js jestjs babeljs


    【解决方案1】:

    当我使用 2 个不同版本的节点打开 2 个终端时,我遇到了这个问题。 (我安装了 NVM 来使用不同版本的节点)

    • 终端 1:npm install(正确,这是节点 14)
    • 终端 2:npm run dev(错误,这是节点 16)

    所以解决方法很简单:

    1. 删除node_modules
    2. 使用相同的节点版本运行 npm installnpm run dev

    【讨论】:

      【解决方案2】:

      你可以通过 npm 试试这个

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

      【讨论】:

        【解决方案3】:

        它对我有用,从 babel 6.x 升级到 7.x 时,我必须在 .bablerc 中将 ['env'] 更改为 ['@babel/preset-env'] - 谢谢!!!

        【讨论】:

          【解决方案4】:

          在 .babelrc 文件的测试部分中,您不小心使用了无范围预设 ("presets": ["env"])。这曾经是 Babel 7 和 @babel 范围之前的方式。不仅如此,由于您已经将@babel/preset-env 配置为全局预设,因此您无需为测试环境再次定义它。如下删除整个测试环境配置应该可以解决您的问题:

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

          【讨论】:

            猜你喜欢
            • 2021-03-03
            • 2021-05-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-18
            • 2018-04-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多