ReferenceError: regeneratorRuntime is not defined (but working inside a scope)

前端 未结 3 1744
借酒劲吻你
借酒劲吻你 2021-02-07 02:53

I\' ve come across this strange occurrence of:

ReferenceError: regeneratorRuntime is not defined

... which I\'ve managed to reproduce in a very minima

相关标签:
3条回答
  • 2021-02-07 02:56

    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');
    
    0 讨论(0)
  • 2021-02-07 03:05

    Also you could do the following with es2015 preset and transform-regenerator plugin:

    .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-regenerator npm package.

    0 讨论(0)
  • 2021-02-07 03:17

    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

    import "core-js/stable";
    import "regenerator-runtime/runtime";
    

    you can find it on the official babeljs documetation orat this article

    0 讨论(0)
提交回复
热议问题