我正在尝试在Babel 6上从头开始使用async/await,但我得到的是regeneratorRuntime没有定义。

.babelrc文件

{
    "presets": [ "es2015", "stage-0" ]
}

package.json文件

"devDependencies": {
    "babel-core": "^6.0.20",
    "babel-preset-es2015": "^6.0.15",
    "babel-preset-stage-0": "^6.0.15"
}

.js文件

"use strict";
async function foo() {
  await bar();
}
function bar() { }
exports.default = foo;

在没有async/await的情况下正常使用它,效果很好。知道我做错了什么吗?


当前回答

上面有这么多答案,我会把我的答案贴出来供参考。我使用webpack并做出反应,这是我没有.babelrc文件的解决方案

我将于2020年8月对此进行研究

安装react和babel

npm i @babel/core babel-loader @babel/preset-env @babel/preset-react react react-dom @babel/plugin-transform-runtime --save-dev

然后在我的webpack.config.js中

// other stuff
module.exports = {
// other stuff

   module: {
   rules: [
  
   {
    test: /\.m?js$/,
    exclude: /(node_modules|bower_components)/,
    use: {
      loader: 'babel-loader',
      options: {
        presets: ['@babel/preset-env',"@babel/preset-react"],
        plugins: ['@babel/plugin-proposal-class-properties', '@babel/plugin-transform-runtime'],
        //npm install --save-dev @babel/plugin-transform-runtime
          }
        }
      },
    ],
  },

};

我只是不知道为什么我现在不需要安装异步包

其他回答

我需要支持的目标浏览器已经支持异步/等待,但是在编写mocha测试时,如果没有正确的设置,我仍然会遇到这个错误。

我在谷歌上搜索的大多数文章都过时了,包括这里的公认答案和高投票率答案,即你不需要polyfill、babel再生器运行时、babel插件转换运行时。如果您的目标浏览器已经支持异步/等待(当然,如果您不需要polyfill)

我也不想使用webpack。

泰勒·龙的答案实际上是正确的,因为他建议babel预置env(但我先省略了它,因为他在开头提到了polifill)。我仍然得到了ReferenceError:regeneratorRuntime最初没有定义,然后我意识到这是因为我没有设置目标。为节点设置目标后,我修复了再生器运行时错误:

  "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
      }]
    ]
  }

此解决方案已过时。

我在这个视频的youtube评论中找到了解决方案https://www.youtube.com/watch?v=iWUR04B42Hc&lc=Ugyq8UJq-OyOzsKIIrB4AaABAg公司

这应该指向正确的注释。“贝丝w”找到了解决方案。

Beth W 3个月前(编辑)我在2019年不得不做的另一个改变是,babel显然不再使用v7之前的阶段0预设,所以在26:15时,我不得不做的不是“npm install--save dev babel polyfill babel-preset-stage-0”,而是:npm安装--保存@babel/polyfill这包括两个旧选项。然后,在应用程序的入口点中,我>包含了“@babel/polyfill”,并在查询预设中保持原样。因此,webpack配置最终看起来像:

const path = require('path');
module.exports = {
    entry: {
        app: ['@babel/polyfill', './src/app.js']
    },
    output: {
        path: path.resolve(__dirname, 'build'),
        filename: 'app.bundle.js'
    },
    mode: 'development',
    module: {
        rules: [{
            test: /\.js?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            query: {
                presets: ['@babel/preset-env']
            }
        }]
    }
}

希望这对某人有所帮助!

我在Chrome中遇到了这个问题。与RieneVaPlu的回答类似,这为我解决了问题:

npm install --save-dev regenerator-runtime

然后在我的代码中:

import 'regenerator-runtime/runtime';

很高兴避免babel polyfill额外的200 kB。

我在将项目转换为typescript项目后开始出现此错误。据我所知,问题源于异步/等待未被识别。

对我来说,通过在我的设置中添加两项内容,错误得到了修复:

正如上面多次提到的,我需要将babel polyfill添加到我的webpack条目数组中:...条目:['abel-polyfill','./index.js'],...我需要更新我的.babelrc,以允许将异步/等待编译为生成器:{“预设”:[“es2015”],“插件”:[“将异步转换为生成器”]}

开发依赖项:

我还必须在package.json文件中的devDependencies中安装一些东西。也就是说,我错过了babel插件将异步转换为生成器、babel polyfill和babel-preset-es2015:

 "devDependencies": {
    "babel-loader": "^6.2.2",
    "babel-plugin-transform-async-to-generator": "^6.5.0",
    "babel-polyfill": "^6.5.0",
    "babel-preset-es2015": "^6.5.0",
    "webpack": "^1.12.13"
 }

完整代码提示:

我从一个非常有用和简洁的GitHub要点中得到了代码,你可以在这里找到。

更新:Babel 7的帖子也有更深入的答案。


Babel 7.4.0或更高版本(核心js 2/3)

从Babel 7.4.0开始,@Babel/polyfill已弃用。

通常,有两种方法安装polyfills/reduator:通过全局命名空间(选项1)或作为ponyfill(选项2,无全局污染)。


选项1:@babel/preset-env

presets: [
  ["@babel/preset-env", {
    "useBuiltIns": "usage",
    "corejs": 3, // or 2,
    "targets": {
        "firefox": "64", // or whatever target to choose .    
    },
  }]
]

将根据您的目标自动使用再生器运行时和核心js。无需手动导入任何内容。不要忘记安装运行时依赖项:

npm i --save regenerator-runtime core-js

或者,设置useBuiltIns:“entry”并手动导入:

import "regenerator-runtime/runtime";
import "core-js/stable"; // if polyfills are also needed

选项2:@babel/transform runtime和@babel/runtime

这种替代方案没有全球范围的污染,适用于图书馆。

{
  "plugins": [
    [
      "@babel/plugin-transform-runtime",
      {
        "regenerator": true,
        "corejs": 3 // or 2; if polyfills needed
        ...
      }
    ]
  ]
}
Install it:
npm i -D @babel/plugin-transform-runtime
npm i @babel/runtime

如果使用了corejs polyfill,则将@babel/runtime替换为@babel/runtime-corejs2(对于“corejs”:2)或@babel/runtime-corejs3(对于“corejs”:3)。