2

I learnt how to use promises in a browser but when I want to use it in my node.js it throw error.

 var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(url) {
                                           ^
ReferenceError: regeneratorRuntime is not defined

node version

v10.4.1

I use babel for export and import syntax with settings like here in starting file

require('babel-register')({ 
    presets: [ 'env' ]
})
czlowiek488
  • 187
  • 2
  • 17

3 Answers3

3

babel-polyfill is required. You must also install it in order to get async/await working.

Here explain better Babel 6 regeneratorRuntime is not defined

Alex Montoya
  • 4,697
  • 1
  • 30
  • 31
  • @AlexMontoya why do we need this to use async/await on Node 10 when it's natively supported? – HRK44 Nov 19 '18 at 17:04
3

I was unable to use async/await and for me this worked out.

In your babel file include this.

presets: [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "10"
        }
      }
    ]  ]
J.F.
  • 13,927
  • 9
  • 27
  • 65
1

Presumably you are trying to use async / await syntax? You will need to install transform-async-to-generator plugin and include it in your Babel config

require('babel-register')({
  presets: ['env'],
  plugins: ['transform-async-to-generator']
})
James
  • 80,725
  • 18
  • 167
  • 237
  • 1
    why do we need this to use async/await on Node 10 when it's natively supported? – HRK44 Nov 19 '18 at 17:04
  • @HRK44 because unless you stipulate otherwise, Babel will transpile modern features for backward compatibility – James Nov 19 '18 at 21:20
  • how to "stipulate otherwise" ? :D – HRK44 Nov 20 '18 at 00:37
  • @HRK44 by setting the correct [target](https://babeljs.io/docs/en/babel-preset-env#targets) for your environment. – James Nov 20 '18 at 00:47
  • thanks I'll look into it, but I think I have it already set up as node 10. I'll double check – HRK44 Nov 20 '18 at 00:59
  • @HRK44 did it work for you with target set as node10, It did not work for me. – Yalamber Jan 05 '19 at 12:34
  • @James I used following `"babel": { "presets": [ [ "@babel/preset-env", { "targets": { "node": "current" }, "useBuiltIns": "usage" } ] ] }` – Yalamber Jan 05 '19 at 14:33
  • 2
    OK it worked I seems like issue was in my build script where I was building using npm script `babel src --out-dir build --presets=@babel/preset-env` I just remove --presets part and it worked ok – Yalamber Jan 05 '19 at 14:43
  • @Yalamber no problem, was just racking my brains unless there was something else missing but your config looked good. Glad you got it working :) If you inspect your compiled file now you should see `async` / `await` will be in there and not transpiled. – James Jan 05 '19 at 14:47
  • @Yalamber I think it did not work for me, I ended up using some other kind of babel plugins – HRK44 Jan 05 '19 at 22:45
  • @HRK44 try looking in to all possible configuration of babel, if you have set proper target it should work. It woked for me by setting target to node current as my current node version supports async await out of the box. – Yalamber Jan 06 '19 at 09:05
  • @james indeed, it worked after I removed the preset part from the build script. seems like it was overriding the configuration. – Yalamber Jan 06 '19 at 09:05