Babel 6 regeneratorRuntime is not defined

前端 未结 30 1848
暖寄归人
暖寄归人 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:10

    I fixed this error by installing babel-polyfill

    npm install babel-polyfill --save
    

    then I imported it in my app entry point

    import http from 'http';
    import config from 'dotenv';
    import 'babel-polyfill';
    import { register } from 'babel-core';
    import app from '../app';
    

    for testing I included --require babel-polyfill in my test script

    "test": "export NODE_ENV=test|| SET NODE_ENV=test&& mocha --compilers 
      js:babel-core/register --require babel-polyfill server/test/**.js --exit"
    
    0 讨论(0)
  • 2020-11-22 04:10

    For people looking to use the babel-polyfill version 7^ do this with webpack ver3^.

    Npm install the module npm i -D @babel/polyfill

    Then in your webpack file in your entry point do this

    entry: ['@babel/polyfill', path.resolve(APP_DIR, 'App.js')],
    
    0 讨论(0)
  • 2020-11-22 04:10

    I had a setup
    with webpack using presets: ['es2015', 'stage-0']
    and mocha that was running tests compiled by webpack.

    To make my async/await in tests work all I had to do is use mocha with the --require babel-polyfill option:

    mocha --require babel-polyfill
    
    0 讨论(0)
  • 2020-11-22 04:14

    To babel7 users and ParcelJS >= 1.10.0 users

    npm i @babel/runtime-corejs2
    npm i --save-dev @babel/plugin-transform-runtime @babel/core
    

    .babelrc

    {
      "plugins": [
        ["@babel/plugin-transform-runtime", {
          "corejs": 2
        }]
      ]
    }
    

    taken from https://github.com/parcel-bundler/parcel/issues/1762

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

    If you using Gulp + Babel for a frontend you need to use babel-polyfill

    npm install babel-polyfill

    and then add a script tag to index.html above all other script tags and reference babel-polyfill from node_modules

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

    You're getting an error because async/await use generators, which are an ES2016 feature, not ES2015. One way to fix this is to install the babel preset for ES2016 (npm install --save babel-preset-es2016) and compile to ES2016 instead of ES2015:

    "presets": [
      "es2016",
      // etc...
    ]
    

    As the other answers mention, you can also use polyfills (though make sure you load the polyfill first before any other code runs). Alternatively, if you don't want to include all of the polyfill dependencies, you can use the babel-regenerator-runtime or the babel-plugin-transform-runtime.

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