139

I was experimenting on using Node version 6.2.1 with some of my code. Had plans to migrate most of the hyper-callback oriented codes to something that looks cleaner and maybe performs better.

I have no clue why, the terminal throws up an error when I try to execute the node code.

helloz.js

(async function testingAsyncAwait() {
    await console.log("Print me!");
})();

Logs-

BOZZMOB-M-T0HZ:rest bozzmob$ node helloz.js 
/Users/bozzmob/Documents/work/nextgennms/rest/helloz.js:1
(function (exports, require, module, __filename, __dirname) { (async function testingAsyncAwait() {
                                                                     ^^^^^^^^
SyntaxError: Unexpected token function
    at Object.exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:513:28)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:456:3
BOZZMOB-M-T0HZ:rest bozzmob$ node -v
v6.2.1

What am I missing? Please throw me some light on the same.


Update 1:

I tried to use Babel as Quentin suggested, But, I am getting the following error still.

Updated Code-

require("babel-core/register");
require("babel-polyfill");

    (async function testingAsyncAwait() {
        await console.log("Print me!");
    })();

Logs-

BOZZMOB-M-T0HZ:rest bozzmob$ babel helloz.js > helloz.trans.js
SyntaxError: helloz.js: Unexpected token (3:7)
  1 | require("babel-polyfill");
  2 | 
> 3 | (async function testingAsyncAwait() {
    |        ^
  4 |     await console.log("Print me!");
  5 | })();
Mike Bonnell
  • 16,181
  • 3
  • 61
  • 77
bozzmob
  • 12,364
  • 16
  • 50
  • 73

7 Answers7

189

Async functions are not supported by Node versions older than version 7.6.

You'll need to transpile your code (e.g. using Babel) to a version of JS that Node understands if you are using an older version.

That said, versions of Node.js which don’t support async functions are now all past End Of Life and are unsupported, so if you are using an earlier version you should very strongly consider upgrading.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
38

Nodejs supports async/await from version 7.6.

Release post: https://v8project.blogspot.com.br/2016/10/v8-release-55.html

31

Node.JS does not fully support ES6 currently, so you can either use asyncawait module or transpile it using Babel.

install

npm install --save asyncawait

helloz.js

var async = require('asyncawait/async');
var await = require('asyncawait/await');

(async (function testingAsyncAwait() {
    await (console.log("Print me!"));
}))();
danronmoon
  • 3,814
  • 5
  • 34
  • 56
Nivesh
  • 2,573
  • 1
  • 20
  • 28
  • 9
    It is vital to use the parenthesis when using the library above (asyncawait) to polyfill await and async. In ES2017, await and async are keywords. In the library above, they are functions. – Phil Nov 26 '16 at 23:01
19

If you are just experimenting you can use babel-node command line tool to try out the new JavaScript features

  1. Install babel-cli into your project

    $ npm install --save-dev babel-cli

  2. Install the presets

    $ npm install --save-dev babel-preset-es2015 babel-preset-es2017

  3. Setup your babel presets

    Create .babelrc in the project root folder with the following contents:

    { "presets": ["es2015","es2017"] }

  4. Run your script with babel-node

    $ babel-node helloz.js

This is only for development and testing but that seems to be what you are doing. In the end you'll want to set up webpack (or something similar) to transpile all your code for production

If you want to run the code somewhere else, webpack can help and here is the simplest configuration I could work out:

stujo
  • 2,089
  • 24
  • 29
  • Clic again and I got the same result. I see a 404 on github ¿? – Oscar Nevarez Aug 30 '17 at 18:11
  • I needed to use ./node_modules/.bin/babel-node helloz.js instead of babel-node helloz.js – Marty Jul 18 '18 at 19:40
  • Hi Marty, I think that depends on if you have babel-node installed globally or just within the package project, in my case I probably had it installed globally – stujo Jul 31 '18 at 22:13
12

node v6.6.0

If you just use in development. You can do this:

npm i babel-cli babel-plugin-transform-async-to-generator babel-polyfill --save-dev

the package.json would be like this:

"devDependencies": {
   "babel-cli": "^6.18.0",
   "babel-plugin-transform-async-to-generator": "^6.16.0",
   "babel-polyfill": "^6.20.0"
}

create .babelrc file and write this:

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

and then, run your async/await script like this:

./node_modules/.bin/babel-node script.js
Lin Du
  • 88,126
  • 95
  • 281
  • 483
4

Though I'm coming in late, what worked for me was to install transform-async-generator and transform-runtime plugin like so:

npm i babel-plugin-transform-async-to-generator babel-plugin-transform-runtime --save-dev

the package.json would be like this:

"devDependencies": {
   "babel-plugin-transform-async-to-generator": "6.24.1",
   "babel-plugin-transform-runtime": "6.23.0"
}

create .babelrc file and write this:

{
  "plugins": ["transform-async-to-generator", 
["transform-runtime", {
      "polyfill": false,
      "regenerator": true
    }]
]
}

and then happy coding with async/await

Theophilus Omoregbee
  • 2,463
  • 1
  • 22
  • 33
  • 1
    if u r using visual studio code for angular, u don't need to do any work after using npm install from above. Everything will be installed and configured automatically, but thank you anyway! – chainstair Feb 24 '19 at 01:03
1

include and specify the node engine version to the latest, say at this time I did add version 8.

{
  "name": "functions",
  "dependencies": {
    "firebase-admin": "~7.3.0",
    "firebase-functions": "^2.2.1",
  },
  "engines": {
    "node": "8"
  },
  "private": true
}

in the following file

package.json

Joseph Wambura
  • 2,732
  • 2
  • 21
  • 24