Babel 6 regeneratorRuntime is not defined

前端 未结 30 1851
暖寄归人
暖寄归人 2020-11-22 03:49

I\'m trying to use async, await from scratch on Babel 6, but I\'m getting regeneratorRuntime is not defined.

.babelrc file

{
    \"presets\": [ \"es2         


        
相关标签:
30条回答
  • 2020-11-22 04:17

    This error is caused when async/await functions are used without the proper Babel plugins. As of March 2020, the following should be all you need to do. (@babel/polyfill and a lot of the accepted solutions have been deprecated in Babel. Read more in the Babel docs.)

    In the command line, type:

    npm install --save-dev @babel/plugin-transform-runtime
    

    In your babel.config.js file, add this plugin @babel/plugin-transform-runtime. Note: The below example includes the other presets and plugins I have for a small React/Node/Express project I worked on recently:

    module.exports = {
      presets: ['@babel/preset-react', '@babel/preset-env'],
      plugins: ['@babel/plugin-proposal-class-properties', 
      '@babel/plugin-transform-runtime'],
    };
    
    0 讨论(0)
  • 2020-11-22 04:17

    babel-regenerator-runtime is now deprecated, instead one should use regenerator-runtime.

    To use the runtime generator with webpack and babel v7:

    install regenerator-runtime:

    npm i -D regenerator-runtime
    

    And then add within webpack configuration :

    entry: [
      'regenerator-runtime/runtime',
      YOUR_APP_ENTRY
    ]
    
    0 讨论(0)
  • 2020-11-22 04:17

    I had this problem in Chrome. Similar to RienNeVaPlu͢s’s answer, this solved it for me:

    npm install --save-dev regenerator-runtime
    

    Then in my code:

    import 'regenerator-runtime/runtime';
    

    Happy to avoid the extra 200 kB from babel-polyfill.

    0 讨论(0)
  • 2020-11-22 04:17

    The targeted browsers I need to support already support async/await, but when writing mocha tests, without the proper setting I still got this error.

    Most of the articles I googled are outdated, including the accepted answer and high voted answers here, i.e. you don't need polyfill, babel-regenerator-runtime, babel-plugin-transform-runtime. etc. if your target browser(s) already supports async/await (of course if not you need polyfill)

    I don't want to use webpack either.

    Tyler Long's answer is actually on the right track since he suggested babel-preset-env (but I omitted it first as he mentioned polifill at the beginning). I still got the ReferenceError: regeneratorRuntime is not defined at the first then I realized it was because I didn't set the target. After setting the target for node I fix the regeneratorRuntime error:

      "scripts": {
        //"test": "mocha --compilers js:babel-core/register"
        //https://github.com/mochajs/mocha/wiki/compilers-deprecation
        "test": "mocha --require babel-core/register"
      },
      "devDependencies": {
        "babel-core": "^6.26.3",
        "babel-preset-env": "^1.7.0",
        "mocha": "^5.2.0"
      },
      //better to set it .bablerc, I list it here for brevity and it works too.
      "babel": {
        "presets": [
          ["env",{
            "targets": {
              "node": "current"
               "chrome": 66,
               "firefox": 60,
            },
            "debug":true
          }]
        ]
      }
    
    0 讨论(0)
  • 2020-11-22 04:17

    If you are building an app, you just need the @babel/preset-env and @babel/polyfill:

    npm i -D @babel/preset-env
    npm i @babel/polyfill
    

    (Note: you don't need to install the core-js and regenerator-runtime packages because they both will have been installed by the @babel/polyfill)

    Then in .babelrc:

    {
      "presets": [
        [
          "@babel/preset-env",
          {
            "useBuiltIns": "entry"  // this is the key. use 'usage' for further codesize reduction, but it's still 'experimental'
          }
        ]
      ]
    }
    

    Now set your target environments. Here, we do it in the .browserslistrc file:

    # Browsers that we support
    
    >0.2%
    not dead
    not ie <= 11
    not op_mini all
    

    Finally, if you went with useBuiltIns: "entry", put import @babel/polyfill at the top of your entry file. Otherwise, you are done.

    Using this method will selectively import those polyfills and the 'regenerator-runtime' file(fixing your regeneratorRuntime is not defined issue here) ONLY if they are needed by any of your target environments/browsers.

    0 讨论(0)
  • 2020-11-22 04:18

    Most of these answers recommend solutions for dealing with this error using WebPack. But in case anyone is using RollUp (like I am), here is what finally worked for me (just a heads-up and bundling this polyfill ads about 10k tot he output size):

    .babelrc

    {
        "presets": [
            [
                "env",
                {
                    "modules": false,
                    "targets": {
                        "browsers": ["last 2 versions"]
                    }
                }
            ]
        ],
        "plugins": ["external-helpers",
            [
                "transform-runtime",
                {
                    "polyfill": false,
                    "regenerator": true
                }
            ]]
    }
    

    rollup.config.js

    import resolve from 'rollup-plugin-node-resolve';
    import babel from 'rollup-plugin-babel';
    import uglify from 'rollup-plugin-uglify';
    import commonjs from 'rollup-plugin-commonjs';
    
    
    export default {
        input: 'src/entry.js',
        output: {
            file: 'dist/bundle.js',
            format: 'umd',
            name: 'MyCoolLib',
            exports: 'named'
        },
        sourcemap: true,
        plugins: [
            commonjs({
                // polyfill async/await
                'node_modules/babel-runtime/helpers/asyncToGenerator.js': ['default']
            }),
            resolve(),
            babel({
                runtimeHelpers: true,
                exclude: 'node_modules/**', // only transpile our source code
            }),
            uglify()
    
        ]
    };
    
    0 讨论(0)
提交回复
热议问题