16

Im having trouble getting async / await transforms working. What am I missing?

My .babelrc:

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

My package.json (snipped):

{  
  "babel-core": "^6.1.2",
  "babel-plugin-transform-runtime": "^6.1.2",
  "babel-preset-es2015": "^6.1.2",
  "babel-preset-stage-0": "^6.1.2"
}

Output:

babel src/server
SyntaxError: src/server/index.js: Unexpected token (7:21)
   5 |
   6 | try {
>  7 |   let server = await server('localhost', env.NODE_PORT || 3000)
     |                      ^
   8 |   console.log(`Server started on ${server.info.uri}`)
   9 | } catch (err) {
  10 |   console.error('Error starting server: ', err)
Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
legomind
  • 241
  • 1
  • 2
  • 7
  • 2
    Your configuration may be limiting loaders to a specific folder like 'client'. Check the includes options for the loader. – jrhicks Mar 15 '16 at 14:44

5 Answers5

12

According to this post you need to have babel-polyfill

Babel 6 regeneratorRuntime is not defined with async/await

Hopefully it'll help you :)

EDIT:

It doesn't have to be babel-polyfill but it's the only one I used.

As Gothdo said: the await keyword has to be in a function scope. Moreover, this function definition has to have the async keyword.

This means that you can not have the await keyword on the top-level scope.

Community
  • 1
  • 1
Yormi
  • 479
  • 1
  • 5
  • 14
  • I am already requiring the package babel-polyfill, it still errors out – legomind Nov 11 '15 at 12:27
  • 12
    Is your `await` line in a function scope ? I'm not sure but it seems that it's not working outside of a function marked with `async`. – Yormi Nov 12 '15 at 01:03
  • @legomind but it is not listed in your package.json (that you posted at least) `"babel-polyfill": "^6.0.16",` – BrunoLM Dec 15 '15 at 21:51
  • Hello. @legomind, did you manage to get this working somehow? – Amir Eldor Dec 17 '15 at 13:21
  • 3
    Just using babel with stage-0 and es2015 preset works for me as long as you import `babel-polyfill` in your entry point. As BrunoLM said, make sure you have the module installed ;) – Yormi Dec 18 '15 at 01:25
2

Looks like async/await is only available in babel-preset-stage-3

http://babeljs.io/docs/plugins/preset-stage-3/

Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
  • 13
    Excuse me, but isn't [stage-0](http://babeljs.io/docs/plugins/preset-stage-0/) supposed to contain stage-3? – Colliot May 09 '16 at 20:19
0

You can compile them yourself using the transform-async-to-module-method plugin, this allows you to compile them down to bluebird co-routines which requires ES6 generators (available in node4).

Or if you need to compile it back to ES5 so it's compatible for browsers you can use transform-async-to-generator and facebook's regenerator.

I've written about how to set up your babel config here http://madole.xyz/async-await-es7/

Madole
  • 1
  • 1
0

Use the Async to generator transform.

Installation

$ npm install babel-plugin-transform-async-to-generator

Usage

Add the following line to your .babelrc file:

{
  "plugins": ["transform-async-to-generator"]
}
Beder Acosta Borges
  • 5,238
  • 2
  • 28
  • 23
0

It's recommended to upgrade to Babel 7 and use babel-env as opposed to stages (see here: https://github.com/babel/babel-upgrade).

There's a command you can use to upgrade accordingly:

npx babel-upgrade
Tamizaan
  • 11
  • 2