ReferenceError:未定义regeneratorRuntime(但在范围内工作) [英] ReferenceError: regeneratorRuntime is not defined (but working inside a scope)

查看:264
本文介绍了ReferenceError:未定义regeneratorRuntime(但在范围内工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过以下奇怪的情况:

ReferenceError: regeneratorRuntime is not defined

...我设法在一个非常小的设置下进行复制(与同一问题上的类似SO问题相比),并且还注意到了一些奇怪的行为,具体取决于是否使用了作用域.

以下代码有效:

'use strict';

require('babel-polyfill');

{  // scope A (if you remove it you observe different behavior when .babelrc is present)

    function *simplestIterator() {
        yield 42;
    }

    for (let v of simplestIterator()) {
        console.log(v);
    }

}

包装为:

$ npm ls --depth 0
simple-babel-serverside-node-only-archetype@1.0.0 /home/mperdikeas/regeneratorRuntimeNotDefined
├── babel-cli@6.7.5
├── babel-core@6.7.6
├── babel-polyfill@6.7.4
├── babel-preset-es2016@6.0.11
└── babel-runtime@6.6.1

.babelrc的内容是:

$ cat .babelrc 
{
    "presets": ["es2016"]
}

但是,当删除作用域并将simplestIterator放在全局作用域上时,它将失败,并显示以下信息:

ReferenceError: regeneratorRuntime is not defined

更奇怪的是,如果删除/重命名了.babelrc文件,则无论范围是否存在,代码都会成功.顺便说一句,无论是作用域还是封装生成器的IIFE都没有区别.

此处的最小github回购.

观察行为:

git clone https://github.com/mperdikeas/regeneratorRuntimeNotDefined.git
cd regeneratorRuntimeNotDefined/
npm install
npm run build && npm run start

以上将在控制台上输出42.现在删除范围,看看会发生什么.然后重命名.babelrc即可再次看到它(有或没有作用域).

我的问题是:

  • es2016 Babel预设为何会触发此错误
  • 为什么将生成器放在示波器中可以解决问题?

更新

基于已接受的答案,并且由于这是我正在编写的模块的代码,所以我最终做了:

require('babel-polyfill');
module.exports = require('./app.js');

解决方案

Babel假定polyfill将在应用程序中的其他任何东西之前加载,但是您使用的是悬挂的函数声明,这意味着它存在并且在 require被调用之前可用.

对于发电机,则需要由polyfill提供的regeneratorRuntime,但是在初始化再生器时,polyfill尚未加载.

Babel团队的建议是制作两个文件:

index.js

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

I' ve come across this strange occurrence of:

ReferenceError: regeneratorRuntime is not defined

... which I've managed to reproduce in a very minimal setting (compared to similar SO questions on the same issue), and also noticed some weird behavior depending on whether scopes are used.

The following code works:

'use strict';

require('babel-polyfill');

{  // scope A (if you remove it you observe different behavior when .babelrc is present)

    function *simplestIterator() {
        yield 42;
    }

    for (let v of simplestIterator()) {
        console.log(v);
    }

}

Packages are:

$ npm ls --depth 0
simple-babel-serverside-node-only-archetype@1.0.0 /home/mperdikeas/regeneratorRuntimeNotDefined
├── babel-cli@6.7.5
├── babel-core@6.7.6
├── babel-polyfill@6.7.4
├── babel-preset-es2016@6.0.11
└── babel-runtime@6.6.1

Contents of .babelrc are:

$ cat .babelrc 
{
    "presets": ["es2016"]
}

However, when the scope is removed and the simplestIterator is placed on the global scope it fails with:

ReferenceError: regeneratorRuntime is not defined

Even more strangely, if the .babelrc file is removed/renamed the code succeeds whether the scope is present or not. BTW whether it is scope or an IIFE that encapsulates the generator makes no difference.

Minimal github repo demonstrating this behavior here.

To observe the behavior:

git clone https://github.com/mperdikeas/regeneratorRuntimeNotDefined.git
cd regeneratorRuntimeNotDefined/
npm install
npm run build && npm run start

The above will output 42 on the console. Now remove the scope and see what happens. Then rename .babelrc to see it working again (with or without scope).

My questions are:

  • why does the es2016 Babel preset trigger this error
  • why does putting the generator in a scope solves the problem?

update

Based on the accepted answer, and since this was code for a module I was writing I ended up doing:

require('babel-polyfill');
module.exports = require('./app.js');

解决方案

Babel assumes that the polyfill will be loaded before anything else in your application, but you're using a function declaration, which is hoisted, meaning that it exists and is usable before require has been called.

In the case of generators, then need regeneratorRuntime which is provided by the polyfill, but the polyfill hasn't loaded when the regenerator is initialized.

The Babel team's recommendation is to make two files:

index.js

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

这篇关于ReferenceError:未定义regeneratorRuntime(但在范围内工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆