Babel与NodeJS不起作用的传播算子

时间:2019-07-05 10:05:10

标签: javascript node.js webpack babel babel-loader

我使用的是Babel(7.5.0)和最新的NodeJS(12.x),所以NodeJS已经处理了价差运算符,我不想让Babel弄乱它。

这是我的.babelrc:

{
    "plugins": [
        "@babel/plugin-proposal-class-properties",
        "@babel/plugin-transform-runtime"
    ],
    "presets": [
        [
            "@babel/preset-env",
            {
                "modules": "commonjs",
                "targets": {
                    "node": "current"
                },
                "useBuiltIns": "usage"
            }
        ]
    ]
}

这是我的错误:

ERROR in ./src/utils/logger/logger.js
Module build failed: ReferenceError: Unknown helper objectSpread2
    at loadHelper (/app/node_modules/@babel/helpers/lib/index.js:238:27)
    at Object.getDependencies (/app/node_modules/@babel/helpers/lib/index.js:279:21)
    at File.addHelper (/app/node_modules/@babel/core/lib/transformation/file/file.js:204:33)
    at PluginPass.addHelper (/app/node_modules/@babel/core/lib/transformation/plugin-pass.js:31:22)
    at PluginPass.ObjectExpression (/app/node_modules/@babel/plugin-proposal-object-rest-spread/lib/index.js:379:25)
    at newFn (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/visitors.js:193:21)
    at NodePath._call (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:53:20)
    at NodePath.call (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:40:17)
    at NodePath.visit (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:88:12)
    at TraversalContext.visitQueue (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:118:16)
    at TraversalContext.visitMultiple (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:85:17)
    at TraversalContext.visit (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:144:19)
    at Function.traverse.node (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/index.js:94:17)
    at NodePath.visit (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:95:18)
    at TraversalContext.visitQueue (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:118:16)
    at TraversalContext.visitMultiple (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:85:17)
    at TraversalContext.visit (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:144:19)
    at Function.traverse.node (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/index.js:94:17)
    at NodePath.visit (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:95:18)
    at TraversalContext.visitQueue (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:118:16)
    at TraversalContext.visitSingle (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:90:19)
    at TraversalContext.visit (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:146:19)
    at Function.traverse.node (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/index.js:94:17)
    at NodePath.visit (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:95:18)
    at TraversalContext.visitQueue (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:118:16)
    at TraversalContext.visitMultiple (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:85:17)
    at TraversalContext.visit (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:144:19)
    at Function.traverse.node (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/index.js:94:17)
    at NodePath.visit (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:95:18)
    at TraversalContext.visitQueue (/app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:118:16)
 @ ./src/app.js 5:0-40
 @ multi ./src/app.js

我已经读到我需要安装一些插件,但是我认为这是针对较旧的Node.js版本的,我不明白为什么我需要针对内置Node.js功能的插件。

我该怎么做?

更新

我在那里创建了一个问题:https://github.com/babel/babel-loader/issues/798

1 个答案:

答案 0 :(得分:7)

这里是通天塔的维护者。

我们在v7.5.0中有一个错误(很快将在v7.5.1中修复)。您有两种可能的解决方法:

  1. 运行npm install @babel/helpers(或使用yarn):这将迫使npm下载@babel/helpers v7.5.0,其中包含缺少的帮助程序。 如果您还使用@babel/transform-runtime,请确保@babel/runtime为v7.5.0
  2. 如果使用的是yarn,则可以将@babel/plugin-proposal-object-rest-spread锁定到不受该问题影响的旧版本。将此代码添加到您的package.json:

    "resolutions": {
      "**/@babel/plugin-proposal-object-rest-spread": "7.4.4"
    }
    

编辑,该问题已在v7.5.1中修复,请更新