Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

avoiding the import "regenerator-runtime/runtime"

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

like image 292
Rodrigo Avatar asked Dec 23 '22 16:12

Rodrigo


1 Answers

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.


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.
like image 76
Connor Low Avatar answered Jan 02 '23 04:01

Connor Low