Babel 7-ReferenceError:未定义regeneratorRuntime

我有一个应用程序,它是节点后端和React前端。

当我尝试构建/运行节点应用程序时出现以下错误。

节点: v10.13.0

错误:

dist / index.js:314 regeneratorRuntime.mark(function _callee(productId){^

ReferenceError:未定义regeneratorRuntime

.babelrc

{
    "presets": [    [
        "@babel/preset-env", {
          "targets": {
            "node": "current"
          },
        }
      ], "@babel/preset-react"],
    "plugins": [
        "@babel/plugin-proposal-class-properties"
    ]
}

webpack.config.js

{
        mode: "development",
        entry: "./src/index.js",
        target: "node",
        externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
        stats: {
            colors: true
        },
        devtool: "source-map",

        output: {
            path: path.resolve(__dirname, "dist"),
            filename: "index.js",
            sourceMapFilename: "index.js.map"
        },
        module: {
            rules: [
                {
                    enforce: "pre",
                    test: /\.js$/,
                    exclude: /node_modules/,
                    loader: "eslint-loader",
                },
                {
                    test: /\.m?js$/,
                    exclude: /(node_modules|bower_components)/,
                    use: {
                        loader: "babel-loader",
                        options: {
                            presets: ["@babel/preset-env"]
                        }
                    }
                }
            ],
        },
        node: {
            __dirname: false,
            __filename: false,
        },

        "plugins": [
            new CleanWebpackPlugin(),
            new WebpackShellPlugin({
                onBuildStart: [],
                onBuildEnd: ["nodemon dist/index.js"]
            }),

        ]

    },

package.json

 "dependencies": {
    "connect": "^3.6.6",
    "cors": "^2.8.5",
    "dotenv": "^6.1.0",
    "express": "^4.16.4",
    "hellojs": "^1.17.1",
    "i18n-iso-countries": "^3.7.8",
    "morgan": "^1.9.1",
    "react": "^16.6.3",
    "react-dom": "^16.6.3",
    "request": "^2.88.0",
    "request-promise-native": "^1.0.5",
    "serve-static": "^1.13.2",
    "vhost": "^3.0.2"
  },
  "devDependencies": {
    "@babel/cli": "^7.1.5",
    "@babel/core": "^7.1.6",
    "@babel/plugin-proposal-class-properties": "^7.1.0",
    "@babel/preset-env": "^7.1.6",
    "@babel/preset-react": "^7.0.0",
    "babel-eslint": "^10.0.1",
    "babel-loader": "^8.0.4",
    "clean-webpack-plugin": "^1.0.0",
    "copy-webpack-plugin": "^4.6.0",
    "css-loader": "^1.0.1",
    "eslint": "^5.9.0",
    "eslint-config-google": "^0.10.0",
    "eslint-loader": "^2.1.1",
    "eslint-plugin-react": "^7.11.1",
    "extract-loader": "^3.0.0",
    "file-loader": "^2.0.0",
    "node-sass": "^4.10.0",
    "sass-loader": "^7.1.0",
    "style-loader": "^0.23.1",
    "webpack": "^4.26.0",
    "webpack-cli": "^3.1.2",
    "webpack-node-externals": "^1.7.2",
    "webpack-shell-plugin": "^0.5.0"
  }
西门2020/04/03 10:48:50

这里已经有一个很好的答案(最初发布在Babel6问题上),我将翻译为Yarn。基本上,您需要babel运行时(不作为dev依赖项)和插件transform-runtime

yarn add @babel/runtime 
yarn add -D @babel/plugin-transform-runtime

并且,在.babelrc中,添加:

{
    "presets": ["@babel/preset-env"],
    "plugins": [
        ["@babel/transform-runtime"]
    ]
}
阿飞泡芙2020/04/03 10:48:50

我的react项目中出现这个错误,webpack 4这阻止了整个项目的渲染。

这是我解决的方法:

安装plugin-transform-runtime

npm install @babel/plugin-transform-runtime -D

添加plugin-transform-runtime.babelrc文件的插件列表中

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ],
  "plugins": [
    ["@babel/transform-runtime"]  // <= Add it here
  ]  
}
小胖Gil2020/04/03 10:48:50

您将需要具有regeneratorRuntime

安装这两个软件包 -babel-plugin-transform-regeneratorbabel-polyfill

通过添加以下Babel配置 .babelrc

{
  "plugins": ["transform-regenerator"]
}
Mandy村村2020/04/03 10:48:50

React.js用户

如果在使用react时(特别是在尝试使用Async / Wait时遇到此问题,那么Valentino Gagliardi他的博客上提供了有关如何解决此问题的详细方法