Babel CLI ReferenceError: regeneratorRuntime没有定义

时间:2022-07-11 20:24:46

These are my dependencies in package.json :

这些是我在包中的依赖项。json:

"devDependencies": {
  "babel-cli": "^6.24.1",
  "babel-polyfill": "^6.23.0",
  "babel-preset-env": "^1.6.0",
  "babel-preset-stage-0": "^6.5.0",
}

I can compile just fine with "babel server -d transpiled" (I have everything in a server folder instead of src).

我可以使用“babel server -d transp形象”进行编译(我把所有的东西都放在服务器文件夹中,而不是src)。

The problem occurs when I try to run the transpiled code with "node transpiled/index.js". I get

当我尝试使用“节点传输/index.js”运行传输代码时,会出现问题。我得到

ReferenceError: regeneratorRuntime is not defined

ReferenceError: regeneratorRuntime没有定义

I did some searching and it seemed that the issue was that I don't have babel-polyfill when using await/async, but I actually do.

我做了一些搜索,似乎问题是我在使用wait/async时没有babel-polyfill,但实际上我有。

Here is my index.js file

这是我的指数。js文件

require('babel-polyfill');
require('./server');

Here is also my .babelrc file

这也是我的。babelrc文件

{
  "presets": ["env", "stage-0"]
}

What exactly is going on and why am I getting this error? I already have babel-polyfill so this shouldn't be happening.

到底发生了什么,为什么会出现这个错误?我已经有了babel-polyfill软件,所以这不会发生。

1 个解决方案

#1


2  

I ran into the same problem today. According to this issue, the function declarations are hoisted, and they end up before the imports in the transpiled code.

我今天遇到了同样的问题。根据这个问题,函数声明被提升,它们在传输代码的导入之前结束。

To solve this issue you could change the entry point of your application, so that the first file could import the polyfill, and then import the rest of your app. Something like this:

要解决这个问题,可以更改应用程序的入口点,以便第一个文件可以导入polyfill,然后导入应用程序的其余部分。

import 'babel-polyfill';
import './app';

Another solution is to convert your async function declarations to the variable style, so instead of this async myFunction {} you could use this const myFunction = async () => {}. That way, as the function is now a variable, it won't be hoisted before the require("babel-polyfill").

另一种解决方案是将异步函数声明转换为变量样式,因此可以使用这个const myFunction = async() =>{}代替这个async myFunction{}。这样,由于函数现在是一个变量,所以在需要(“babel-polyfill”)之前不会提升它。

#1


2  

I ran into the same problem today. According to this issue, the function declarations are hoisted, and they end up before the imports in the transpiled code.

我今天遇到了同样的问题。根据这个问题,函数声明被提升,它们在传输代码的导入之前结束。

To solve this issue you could change the entry point of your application, so that the first file could import the polyfill, and then import the rest of your app. Something like this:

要解决这个问题,可以更改应用程序的入口点,以便第一个文件可以导入polyfill,然后导入应用程序的其余部分。

import 'babel-polyfill';
import './app';

Another solution is to convert your async function declarations to the variable style, so instead of this async myFunction {} you could use this const myFunction = async () => {}. That way, as the function is now a variable, it won't be hoisted before the require("babel-polyfill").

另一种解决方案是将异步函数声明转换为变量样式,因此可以使用这个const myFunction = async() =>{}代替这个async myFunction{}。这样,由于函数现在是一个变量,所以在需要(“babel-polyfill”)之前不会提升它。