最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - avoiding the import "regenerator-runtimeruntime" - Stack Overflow

programmeradmin2浏览0评论

I am using Parcel to bundle my project and jest to run my tests.

Everything works fine, but on tests that I have the async/await keyword, I had to import regenerator-runtime/runtime

Like this:

import "regenerator-runtime/runtime"

test("read armored key", async() => {

})

And this work.

But without this import (import "regenerator-runtime/runtime") I got this error message:

ReferenceError: regeneratorRuntime is not defined

How can I change my project to run without this import on tests with async?

Example:

I am using Parcel to bundle my project and jest to run my tests.

Everything works fine, but on tests that I have the async/await keyword, I had to import regenerator-runtime/runtime

Like this:

import "regenerator-runtime/runtime"

test("read armored key", async() => {

})

And this work.

But without this import (import "regenerator-runtime/runtime") I got this error message:

ReferenceError: regeneratorRuntime is not defined

How can I change my project to run without this import on tests with async?

Example: https://github.com/skhaz/parcel-regeneratorRuntime-is-not-defined

Share Improve this question edited Jan 20, 2021 at 13:29 Rodrigo asked Jan 19, 2021 at 23:24 RodrigoRodrigo 31113 gold badges72 silver badges157 bronze badges 2
  • 1 if you're targeting newer browsers you could set the browserslist params to target newer browsers. like this in a package.json file: "browserslist": [ "Chrome 70", "Edge 17", "Firefox 68", "Opera 60", "Safari 12" ] ..... google to see how to use browserslist without package.json if your project is set up in another way. – Getter Jetter Commented Jan 20, 2021 at 3:22
  • It's for NodeJS, not for browsers – Rodrigo Commented Jan 20, 2021 at 11:08
Add a comment  | 

3 Answers 3

Reset to default 12 +200

Depending its version, not all features available in your browser runtime will work in the Node runtime. Promises (with await/async) are supported in current versions of node, but since you are using Parcel, which by default uses Babel, your async/await calls will be compiled to use regenerator-runtime, a polyfill for that functionality. You can either import "regenerator-runtime/runtime" in every entry file (not recommended if you don't need the polyfill!), or you can tell babel what your runtime is.

You should be able to get it to work with the @babel/preset-env preset, configured like so in your .babelrc:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "10" // the target node version, boolean true, or "current".
        }
      }
    ]
  ]
}

See this Medium article for more information about configuring babel for this:

I found this answer to my troubles and it worked when I implemented it.


Opinion: Don't rely on zero-configuration tools like Parcel: they end up adding to your development time by creating unexpected behavior (like your issue), or you have to spend time learning how it works. It's enough to debug your own application; you shouldn't have to debug your build tool as well.

As I know that you can configure @babel/preset-env to compile against current node version which is described here then it should work:

.babelrc

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ]
}

When using Webpack, or Parcel, add the below to package.json:

"browserslist": [
  "since 2017-06"
]

or

"browserslist": [
  "last 3 Chrome versions"
]
发布评论

评论列表(0)

  1. 暂无评论