4

Man is this a pain to setup! I've followed the installation instructions here clicking on the nodemon box:

https://babeljs.io/docs/setup/#installation

npm install babel-cli babel-preset-es2015 --save-dev

.babelrc in root directory:

{
  "presets": ["es2015"],
  "plugins": ["transform-async-to-generator"]
}

package.json (I've installed more babel stuff as seen):

...
"devDependencies": {
  "babel-cli": "^6.11.4",
  "babel-core": "^6.13.2",
  "babel-plugin-transform-async-to-generator": "^6.8.0",
  "babel-polyfill": "^6.13.0",
  "babel-preset-es2015": "^6.13.2",
  "babel-preset-node6": "^11.0.0",
  "babel-register": "^6.11.6"
},
"scripts": {
  "startn": "nodemon app.js",
  "babel-node": "babel-node --presets=es2015 --ignore='foo|bar|baz'",
  "babel-dev": "nodemon --exec npm run babel-node -- experiment/socketio/test.js"
},
...

test.js:

(async function () { // <-- error occues here
    const value = await 123;
    console.log(value);
})().then(() => {
    console.log('Done');
});

I run the command run-script babel-dev as seen below. Error:

karl@karl-ux303ln:~/dev/sketch$ npm run-script babel-dev

> sketch@0.0.1 babel-dev /home/karl/dev/sketch
> nodemon --exec npm run babel-node -- experiment/socketio/test.js

[nodemon] 1.10.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `npm run babel-node experiment/socketio/test.js`

> sketch@0.0.1 babel-node /home/karl/dev/sketch
> babel-node --presets=es2015 --ignore='foo|bar|baz' "experiment/socketio/test.js"

/home/karl/dev/sketch/node_modules/babel-core/lib/transformation/file/index.js:591
      throw err;
      ^

SyntaxError: /home/karl/dev/sketch/experiment/socketio/test.js: Unexpected token (1:7)
> 1 | (async function () {
    |        ^
  2 |     const value = await 123;
  3 |     console.log(value);
  4 | })().then(() => {
    at Parser.pp.raise (/home/karl/dev/sketch/node_modules/babylon/lib/parser/location.js:22:13)
    at Parser.pp.unexpected (/home/karl/dev/sketch/node_modules/babylon/lib/parser/util.js:89:8)
    at Parser.pp.expect (/home/karl/dev/sketch/node_modules/babylon/lib/parser/util.js:83:33)
    at Parser.pp.parseParenAndDistinguishExpression (/home/karl/dev/sketch/node_modules/babylon/lib/parser/expression.js:582:12)
    at Parser.pp.parseExprAtom (/home/karl/dev/sketch/node_modules/babylon/lib/parser/expression.js:481:19)
    at Parser.pp.parseExprSubscripts (/home/karl/dev/sketch/node_modules/babylon/lib/parser/expression.js:277:19)
    at Parser.pp.parseMaybeUnary (/home/karl/dev/sketch/node_modules/babylon/lib/parser/expression.js:257:19)
    at Parser.pp.parseExprOps (/home/karl/dev/sketch/node_modules/babylon/lib/parser/expression.js:188:19)
    at Parser.pp.parseMaybeConditional (/home/karl/dev/sketch/node_modules/babylon/lib/parser/expression.js:165:19)
    at Parser.pp.parseMaybeAssign (/home/karl/dev/sketch/node_modules/babylon/lib/parser/expression.js:128:19)

npm ERR! Linux 3.19.0-65-generic
npm ERR! argv "/home/karl/.nvm/versions/node/v6.2.0/bin/node" "/home/karl/.nvm/versions/node/v6.2.0/bin/npm" "run" "babel-node" "experiment/socketio/test.js"
npm ERR! node v6.2.0
npm ERR! npm  v3.8.9
npm ERR! code ELIFECYCLE
npm ERR! sketch@0.0.1 babel-node: `babel-node --presets=es2015 --ignore='foo|bar|baz' "experiment/socketio/test.js"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the sketch@0.0.1 babel-node script 'babel-node --presets=es2015 --ignore='foo|bar|baz' "experiment/socketio/test.js"'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the sketch package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     babel-node --presets=es2015 --ignore='foo|bar|baz' "experiment/socketio/test.js"
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs sketch
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls sketch
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/karl/dev/sketch/npm-debug.log
[nodemon] app crashed - waiting for file changes before starting...

I've also tried switching to node v4.4.7 and upgrading npm to 3.10.6. still same error.

basickarl
  • 37,187
  • 64
  • 214
  • 335
  • `async/await` is not part of ES7, it's a proposal. – Felix Kling Aug 12 '16 at 02:11
  • @FelixKling Indeed it is, but you understand what I mean :) – basickarl Aug 12 '16 at 02:12
  • Please try to avoid using offensive words/phrases in your questions – rossipedia Aug 12 '16 at 03:54
  • @rossipedia I don't find the word "retarded" offensive, it's an interpretive question. There are worse words out there you should worry more about. – basickarl Aug 12 '16 at 03:57
  • Neither do I, honestly. But that really [isn't the point](http://stackoverflow.com/help/be-nice). – rossipedia Aug 12 '16 at 06:31
  • *"The "duplicate post" answer isn't helpful"* and yet you are configuring Babel exactly as described in that post... – Felix Kling Aug 12 '16 at 16:04
  • @FelixKling IF you understand what that post is saying. Sorry Felix, but I don't time to dive into the mechanics of Babel, I just need some ES7 things to work so I can focus on what I DO best. – basickarl Aug 12 '16 at 16:05
  • *"The quickest way to get this working is to use presets which already contain the set of plugins needed to transform ES2015 and newer proposals. For async, you will need the es2015 and stage-3 preset and the runtime plugin."* That's all you needed from that answer. Is it not clear that what means? Either way, glad you figured it out. – Felix Kling Aug 12 '16 at 16:07
  • @FelixKling I'm not saying that the answer presented was *bad*, it just didn't answer *my* question. True it did state those things, however it failed to mention `babel-register` for example, and the presets to be set in `.babelrc`. Also thanks for removing the duplicate, I'll update this question with the appropriate answer now. – basickarl Aug 12 '16 at 16:14

2 Answers2

9

1) remove all babel modules (remove everything, this is a little buggy I've found out)

2) install the following:

npm install --save-dev babel-polyfill babel-preset-es2015 babel-preset-stage-3 babel-register

3) fix .babelrc file:

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

4) check to see if it works (-r flag is to preload modules):

node -r babel-register -r babel-polyfill experiment/socketio/test.js

To fix nodemon:

nodemon -r babel-register -r babel-polyfill experiment/socketio/test.js
basickarl
  • 37,187
  • 64
  • 214
  • 335
1

Async/await is handled in babeljs by the plugin transform-async-to-generator, which is included in the stage-3 preset. It's not included by default in the es2015 preset, so you'll have to add either the plugin itself or the stage-3 preset explicitly. To do that on the command line, change this line in package.json:

"babel-node": "babel-node --presets=es2015 --ignore='foo|bar|baz'",

to read:

"babel-node": "babel-node --presets=es2015,stage-3 --ignore='foo|bar|baz'",

adding the stage-3 preset. The same could also be achieved like so:

"babel-node": "babel-node --presets=es2015 --plugins=transform-async-to-generator --ignore='foo|bar|baz'",

However, it's generally recommended practice to use the .babelrc configuration file, which could be as simple as:

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

or

{
  "presets": ["es2015"],
  "plugins": ["transform-async-to-generator"]
}

and then the line in your package.json could just be:

"babel-node": "babel-node --ignore='foo|bar|baz'",

(original answer for comment context)

You need to add:

"plugins": ["transform-async-to-generator"]

to your .babelrc, as I don't believe async/await is included in any of the standard presets (since it can be implemented in multiple ways)

rossipedia
  • 56,800
  • 10
  • 90
  • 93