【问题标题】:Babel error: "Error: Cannot find module 'babel-plugin-transform-object-rest-spread' from '/vagrant'"Babel 错误:“错误:无法从 '/vagrant' 找到模块 'babel-plugin-transform-object-rest-spread'”
【发布时间】:2019-05-09 06:03:25
【问题描述】:

我正在尝试从 Babel 6 -> 7 升级。我使用 npx babel-upgrade --write 协助更新 package.json,然后运行 ​​npm install

在尝试运行我们的 webpack 开发服务器时,它无法编译并出现以下错误:

ERROR in ./src/app/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module 'babel-plugin-transform-object-rest-spread' from '/vagrant'
    at Function.module.exports [as sync] (/vagrant/node_modules/resolve/lib/sync.js:58:15)
    at resolveStandardizedName (/vagrant/node_modules/@babel/core/lib/config/files/plugins.js:101:31)
    at resolvePlugin (/vagrant/node_modules/@babel/core/lib/config/files/plugins.js:54:10)
    at loadPlugin (/vagrant/node_modules/@babel/core/lib/config/files/plugins.js:62:20)
    at createDescriptor (/vagrant/node_modules/@babel/core/lib/config/config-descriptors.js:154:9)
    at items.map (/vagrant/node_modules/@babel/core/lib/config/config-descriptors.js:109:50)
    at Array.map (<anonymous>)
    at createDescriptors (/vagrant/node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
    at createPluginDescriptors (/vagrant/node_modules/@babel/core/lib/config/config-descriptors.js:105:10)
    at alias (/vagrant/node_modules/@babel/core/lib/config/config-descriptors.js:63:49)

注意/vagrant 是项目根目录。

babel-plugin-transform-object-rest-spread 的唯一引用存在于node_modules 中,所以我猜这些是在以某种方式触发它?

package.json

"devDependencies": {
    "@babel/core": "^7.0.0",
    "@babel/plugin-proposal-object-rest-spread": "^7.0.0",
    "@babel/polyfill": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "@babel/preset-react": "^7.0.0",
    "autoprefixer": "^9.0.1",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^23.4.2",
    "babel-loader": "^8.0.0",
    "babel-plugin-lodash": "^3.3.4",
    "babel-plugin-react-transform": "^3.0.0",
    "css-loader": "^1.0.0",
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-16": "^1.1.1",
    "enzyme-to-json": "^3.3.4",
    "eslint": "^5.2.0",
    "eslint-config-standard": "^11.0.0",
    "eslint-plugin-import": "^2.13.0",
    "eslint-plugin-jsx-a11y": "^6.1.1",
    "eslint-plugin-node": "^7.0.1",
    "eslint-plugin-promise": "^3.8.0",
    "eslint-plugin-react": "^7.10.0",
    "eslint-plugin-standard": "^3.1.0",
    "file-loader": "^1.1.11",
    "html-webpack-plugin": "^3.2.0",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^24.7.1",
    "jest-fetch-mock": "^1.6.5",
    "lodash-webpack-plugin": "^0.11.5",
    "mini-css-extract-plugin": "^0.6.0",
    "path": "^0.12.7",
    "postcss": "^7.0.1",
    "postcss-cssnext": "^3.0.2",
    "postcss-loader": "^2.1.6",
    "postcss-modules-values": "^1.3.0",
    "raw-loader": "^0.5.1",
    "react-test-renderer": "^16.4.1",
    "redux-mock-store": "^1.5.3",
    "sinon": "^6.1.4",
    "standard": "^11.0.0",
    "style-loader": "^0.21.0",
    "svg-react-loader": "^0.4.5",
    "uglifyjs-webpack-plugin": "^1.2.7",
    "webpack": "^4.30.0",
    "webpack-cli": "^3.3.1",
    "webpack-dev-server": "^3.3.1",
    "webpack-merge": "^4.1.3",
    "webpack-visualizer-plugin": "^0.1.11"
  },

.babelrc

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ],
  "plugins": [
    "@babel/plugin-proposal-object-rest-spread"
  ]
}

编辑:我无法确定问题的原因并已恢复到 Babel 6。

【问题讨论】:

  • @babel/plugin-proposal-object-rest-spreadbabel-plugin-transform-object-rest-spread 之间有一个不幸的区别。
  • 我知道它们是不同的,但 Babel 7 似乎不再支持babel-plugin-transform-object-rest-spread。有什么替代方案?还是我错了?
  • 你没有错。一个好的解决方案是将所有依赖项转换为Babel 7。简单的解决方案是安装所有丢失的东西并尝试直到它正常工作¯_(ツ)_/¯
  • 我认为可能是这种情况。不幸的是,有 10 多个软件包依赖它,所以这不是一项小任务。谢谢@Aviad。
  • 尝试使用 yarn why 或 npm 等效项来了解为什么包含它

标签: node.js npm babeljs


【解决方案1】:

遇到了这个问题,经过一番挖掘后能够解决它--

我们的 webpack 配置中的插件和 package.json 中仍然有一个 transform-object-rest-spread,没有 babel 前缀。将其替换为 @babel/plugin-proposal-object-rest-spread 已为我们修复!

【讨论】:

    【解决方案2】:

    根据@user14981617 的建议更新了预设。

    https://babeljs.io/docs/en/env/ 在终端中被推荐。

    npm WARN deprecated babel-preset-es2015@6.24.1: ?  Thanks for using Babel: we recommend using babel-preset-env now: please read https://babeljs.io/env to update!
    

    :)

    这是我目前拥有的:

    {
    "presets": [
        ["env", {
            "modules": false,
            "targets": {
              "chrome": "60",
              "edge": "60",
              "node": "current"
            }
          }],
        "@babel/preset-env",
        "@babel/preset-react"
    ],
    "plugins": [
    "@babel/plugin-proposal-object-rest-spread"]}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 2022-08-15
      • 2021-10-28
      • 2016-03-23
      • 2020-02-02
      • 2019-05-13
      • 1970-01-01
      相关资源
      最近更新 更多