Babel 7 - ReferenceError: regeneratorRuntime 未定义 [英] Babel 7 - ReferenceError: regeneratorRuntime is not defined
问题描述
我有一个应用程序,它是一个节点后端和一个 React 前端.
I have an application that is a node backend and a react frontend.
当我尝试构建/运行我的节点应用程序时出现以下错误.
I get the following error when i try to build/run my node application.
节点:v10.13.0
错误:
dist/index.js:314regeneratorRuntime.mark(function _callee(productId) {^
dist/index.js:314 regeneratorRuntime.mark(function _callee(productId) { ^
ReferenceError: regeneratorRuntime 未定义
ReferenceError: regeneratorRuntime is not defined
.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"
}
推荐答案
更新答案:
如果您使用的是 Babel 7.4.0 或更新版本,那么 @babel/polyfill
已经 已弃用.相反,您需要在主 js 文件(可能是 index.js 或类似文件)的顶部使用以下内容:
If you are using Babel 7.4.0 or newer, then @babel/polyfill
has been deprecated. Instead, you will want to use the following at the top of your main js file (likely index.js or similar):
import "core-js/stable";
import "regenerator-runtime/runtime";
使用 npm 安装这些软件包:
Install these packages either with npm:
npm install --save core-js
npm install --save regenerator-runtime
或用纱线:
yarn add core-js
yarn add regenerator-runtime
原答案:
我刚刚遇到了这个问题,并遇到了以下解决方案:
I just encountered this problem and came across the following solution:
在 package.json 中,我有 @babel/polyfill
作为依赖项.但是,在我的 index.js(我的主要 js 文件)中,我忽略了将以下行放在顶部:
In package.json I had @babel/polyfill
as a dependency. However, in my index.js (My main js file) I had neglected to place the following line at the the top:
导入'@babel/polyfill'
导入后,一切正常.
我不需要像其他答案建议的那样安装 babel-runtime.
I did not need to install babel-runtime as other answers are suggesting.
这篇关于Babel 7 - ReferenceError: regeneratorRuntime 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!