Generators do not work, throws an error

Can't find variable: regeneratorRuntime 

here is the code where the error occurs

  function* generateSequence() { yield 1; yield 2; return 3; } let generator = generateSequence(); let one = generator.next(); console.log(JSON.stringify(one)); 

Here is the compiled webpack code.

 /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = "/static/"; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports) { "use strict"; var _marked = [generateSequence].map(regeneratorRuntime.mark); function generateSequence() { return regeneratorRuntime.wrap(function generateSequence$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return 1; case 2: _context.next = 4; return 2; case 4: return _context.abrupt("return", 3); case 5: case "end": return _context.stop(); } } }, _marked[0], this); } var generator = generateSequence(); var one = generator.next(); console.log(JSON.stringify(one)); /***/ } /******/ ]); //# sourceMappingURL=main.js.map 

Here is part of the webpack config

  loaders: [ { test: /\.jsx?$/, exclude: /node_modules/, plugins: ['transform-runtime'], loader: 'babel-loader', query: {presets: ['es2015', 'react', 'stage-2']} } ] 
  • No need to bring the entire config - leave only the settings babel . Instead, list the file where the error occurs, in two versions (source and compiled). It is advisable to take for the sample file, consisting of one empty generator. - Pavel Mayorov
  • Well, sort of right now, he gave a minimal example. - Egor
  • Read my first comment again. What did I ask to do? - Pavel Mayorov
  • plugins: [["transform-runtime", { "polyfill": false, "regenerator": true }]] - vp_arth
  • If this is added to the loader, then nothing has changed. - Egor

1 answer 1

It was necessary to add transform-regenerator to the plugins

 { test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader', query: { presets: ['es2015', 'react', 'stage-2'], plugins: [ ["transform-runtime"], ["transform-regenerator"] ], } }