Javascript ReferenceError: regeneratorRuntime 未定义(但在范围内工作)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36619383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ReferenceError: regeneratorRuntime is not defined (but working inside a scope)
提问by Marcus Junius Brutus
I' ve come across this strange occurrence of:
我遇到过这种奇怪的情况:
ReferenceError: regeneratorRuntime is not defined
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.
...我设法在一个非常小的设置中重现(与同一问题上的类似 SO 问题相比),并且还注意到一些奇怪的行为,具体取决于是否使用了范围。
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
[email protected] /home/mperdikeas/regeneratorRuntimeNotDefined
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
Contents of .babelrc
are:
内容为.babelrc
:
$ cat .babelrc
{
"presets": ["es2016"]
}
However, when the scope is removed and the simplestIterator
is placed on the global scope it fails with:
但是,当范围被删除并将simplestIterator
放在全局范围内时,它会失败:
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.
更奇怪的是,如果.babelrc
文件被删除/重命名,无论范围是否存在,代码都会成功。顺便说一句,无论是作用域还是封装生成器的 IIFE 都没有区别。
Minimal github repo demonstrating this behavior here.
最小的 github repo在这里展示了这种行为。
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).
以上将42
在控制台上输出。现在移除范围,看看会发生什么。然后重命名.babelrc
以查看它再次工作(有或没有范围)。
My questions are:
我的问题是:
- why does the
es2016
Babel preset trigger this error - why does putting the generator in a scope solves the problem?
- 为什么
es2016
Babel 预设会触发这个错误 - 为什么将生成器放在范围内可以解决问题?
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');
回答by loganfsmyth
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 beforerequire
has been called.
Babel 假设 polyfill 将在您的应用程序中的任何其他内容之前加载,但是您使用的是一个函数声明,该声明被提升,这意味着它存在并且在require
被调用之前可用。
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.
在生成器的情况下,则需要regeneratorRuntime
由 polyfill 提供,但是在 regenerator 初始化时,polyfill 尚未加载。
The Babel team's recommendation is to make two files:
Babel 团队的建议是制作两个文件:
index.js
索引.js
require('babel-polyfill');
require('./app');
回答by Mark Lavrynenko
Also you could do the following with es2015preset and transform-regeneratorplugin:
您也可以使用es2015预设和转换再生器插件执行以下操作:
.babelrc
.babelrc
{
"presets": ["es2015"],
'plugins': [
'transform-regenerator'
]
}
Code
代码
let regeneratorRuntime = require("regenerator-runtime");
// You code with ES6 generators
P.S. Of course you should install babel-plugin-transform-regeneratornpm package.
PS 当然你应该安装babel-plugin-transform-regeneratornpm 包。
回答by Mike Araya
I know this has been answered but, unfortunately, they didn't fix the problem for me.
what solved it was to import babel babel-polyfills
inside the file
我知道这已经得到回答,但不幸的是,他们没有为我解决问题。解决的是babel-polyfills
在文件中导入 babel
import "core-js/stable";
import "regenerator-runtime/runtime";
you can find it on the official babeljs documetationorat this article
你可以在官方的 babeljs 文档或这篇文章中找到它