babel: Babel "babel-plugin-transform-runtime" transpiled file throws "regeneratorRuntime is not defined" error (T6676)
Issue originally made by @d6u
Bug information
- Babel version: 6.2.1
- Node version: 0.10.40
- npm version: 2.14.12
Options
package.json
"dependencies": {
"babel-core": "^6.2.1",
"babel-plugin-transform-runtime": "^6.1.18",
"babel-preset-es2015": "^6.1.18",
"babel-runtime": "6.0.8"
},
"devDependencies": {
"babel-cli": "^6.2.0"
}
.babelrc
{
"presets": ["es2015"],
"plugins": ["transform-runtime"]
}
Terminal
$ babel index.es6 > index.js
$ node index.js
/Users/daiwei/dev/temp/babel6/index.js:57
throw _iteratorError;
^
ReferenceError: regeneratorRuntime is not defined
at iter (/Users/daiwei/dev/temp/babel6/index.js:16:10)
at Object.<anonymous> (/Users/daiwei/dev/temp/babel6/index.js:42:51)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:935:3
Input code
index.es6
function *iter() {
yield 1;
yield 2;
yield 3;
}
for (let n of iter()) {
console.log(n);
}
Description
When transpiling es2015 scripts into es5, and run in node 0.10. regeneratorRuntime
is not defined in transpiled es2015 file. The first few lines of the es5 file looks like:
"use strict";
var _getIterator2 = require("babel-runtime/core-js/get-iterator");
var _getIterator3 = _interopRequireDefault(_getIterator2);
var _regenerator = require("babel-runtime/regenerator");
var _regenerator2 = _interopRequireDefault(_regenerator);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _marked = [iter].map(_regenerator2.default.mark);
function iter() {
return regeneratorRuntime.wrap(function iter$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return 1;
....
....
Where regeneratorRuntime
was never defined. Requiring babel-polyfill
will solve the problem, but requiring babel-ployfill is not necessary in Babel 5. In Babel 5 with --optional runtime
, the output es5 file top looks like:
"use strict";
var _regeneratorRuntime = require("babel-runtime/regenerator")["default"];
var _getIterator = require("babel-runtime/core-js/get-iterator")["default"];
var marked0$0 = [iter].map(_regeneratorRuntime.mark);
function iter() {
return _regeneratorRuntime.wrap(function iter$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.next = 2;
return 1;
You can see clearly _regeneratorRuntime
is defined here.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 34
Another workaround is to add the following to the es6 file using it:
Ran into this bug yesterday. This simple function definition is enough to trigger the miscompilation (using es2015 and stage-0 presets, transform-runtime plugin)
In the compiled output, notice the line
Which should be