Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

babel 6 async / await: Unexpected token

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)
like image 860
legomind Avatar asked Nov 10 '15 23:11

legomind


2 Answers

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.

like image 171
Yormi Avatar answered Sep 21 '22 13:09

Yormi


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

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

like image 43
Allain Lalonde Avatar answered Sep 22 '22 13:09

Allain Lalonde