25

Why am I getting this error When I use async?

My Code:

bot.onText(/\/start/, async  msg => {
  const opts = {
    parse_mode: 'Markdown' ,
    reply_markup: JSON.stringify({
      keyboard: StartKeyboard,
      resize_keyboard: true,
      one_time_keyboard: true
    })
  };
  await bot.sendMessage(msg.chat.id, 'Hi', opts);
});

Error:

bot.onText(/\/start/, async  msg => {
                      ^^^^^
SyntaxError: missing ) after argument list

I'm using node.js v6.11.0 with "dependencies":

{ "babel-polyfill": "^6.23.0",
  "cheerio": "^1.0.0-rc.2",
  "dotenv": "^4.0.0",
  "firebase": "^4.1.2",
  "firebase-admin": "^5.0.0",
  "node-telegram-bot-api": "^0.27.1",
  "request": "^2.81.0" },
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
Saeed Heidarizarei
  • 8,406
  • 21
  • 60
  • 103
  • Does your environment support async/await syntax? – Bergi Sep 19 '17 at 18:00
  • I'm using node.js v6.11.0 with "dependencies": { "babel-polyfill": "^6.23.0", "cheerio": "^1.0.0-rc.2", "dotenv": "^4.0.0", "firebase": "^4.1.2", "firebase-admin": "^5.0.0", "node-telegram-bot-api": "^0.27.1", "request": "^2.81.0" }, – Saeed Heidarizarei Sep 19 '17 at 18:01
  • 6.11.0, That is okey in my playground, but when I want to run with `node index.js` I get This Error, maybe that is for es6 or ... – Saeed Heidarizarei Sep 19 '17 at 18:03
  • 2
    async/await is ES2017. IIRC Node didn't get async/await until version 7.10. –  Sep 19 '17 at 18:05
  • @Amy But with the polyfill that should be solved anyways, right? – faerin Sep 19 '17 at 18:09
  • in my playground every things is okey with `import 'babel-polyfill';` but in `node index.js` I have import error and i Changed that to This `require('babel-polyfill');` but I got async error – Saeed Heidarizarei Sep 19 '17 at 18:09
  • 2
    `babel-polyfill` do not fix syntax errors, just "API" related things (Map, Set, Array...). You need to call `babel` to transform you code to a runnable version. I usually use `babel-node` to do so. – NeoPix Sep 19 '17 at 18:11
  • 1
    That would work if you run the code through babel first to generate ES6, before running it with Node. –  Sep 19 '17 at 18:11
  • 1
    @Amy I think it's async/await is enabled by default from **Node v7.6**. Sedric Could you bump up your node version to > 8.x? If you can [tj/n](https://github.com/tj/n) - node's version manager will make it super easy. – Mihailo Sep 19 '17 at 18:12
  • In windows 64 latest version is 6.11.3 in official website – Saeed Heidarizarei Sep 19 '17 at 18:14
  • 1
    @SedricHeidarizarei no, that's the latest LTS release. the current release is 8.5. – Kevin B Sep 19 '17 at 18:16
  • 1
    Ah tj/n wont work on windows then. But you should be able to download the v8 from the site if you chose **Current** [Link](https://nodejs.org/en/download/current/) – Mihailo Sep 19 '17 at 18:16
  • Oh Thank you, I will install that now. – Saeed Heidarizarei Sep 19 '17 at 18:17

3 Answers3

22

Your version of NodeJS (6.11 LTS) is too old and does not support the async/await features. The syntax error is a result of the Javascript interpreter not recognizing the async token and getting confused about arguments.

Upgrade to NodeJS 7.6 or later. https://www.infoq.com/news/2017/02/node-76-async-await

In prior versions, the only way to perform asynchronous behaviour is to use promises.

Soviut
  • 88,194
  • 49
  • 192
  • 260
  • Is there any way to use async in node.js 6? Because I want to run in android server with `dory node` application – Saeed Heidarizarei Sep 19 '17 at 18:26
  • 1
    There are other ways to do asynchronous work, such as promises or streams. However, the `async` and `await` keywords cannot be used in older versions. – Soviut Sep 19 '17 at 18:29
  • 1
    Still getting this error on: Node.js v14.15.0. – Kristian Feb 23 '21 at 19:31
  • @Kristian are you sure this is happening in an async/await situation or do you just have a syntax error? – Soviut Feb 24 '21 at 04:47
  • @Kristian I've added an answer which may be helpful to people seeing this on newer versions of Node. – sigint Oct 18 '21 at 10:01
6

If you don't want to/can't update your node version, try using babel presets. I had the same error using ES6 with jest (node v6.9.1).

Just add these two modules to your dependencies

npm install --save babel-preset-es2015 babel-preset-stage-0

And add a file .babelrc to your root dir with the following code:

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

And if you are not using it already, install babel-cli and run your application with babel-node command

sudo npm install -g babel-cli

babel-node app.js
Thiago Loddi
  • 2,212
  • 4
  • 21
  • 35
4

If you're seeing this error with a newer version of Node, it's probably a syntax or some other error before the line Node is pointing out.

For instance, consider the snippet below.

router.get("/", function (req, res, next) {
    try {
        res.json(await mySvc.myFunc());
    } catch (err) {
        console.error(err.message);
        next(err);
    }
});

With node -v reporting v14.17.6, this gives:

myapp $ DEBUG=myapp:* npm start

> myapp@0.0.0 start /home/me/myapp
> node ./bin/www

/home/me/myapp/routes/myroute.js:7
        res.json(await mySvc.myFunc());
                 ^^^^^

SyntaxError: missing ) after argument list

The error, of course, is on the first line of the snippet. Adding an async on that line, thus,

router.get("/", async function (req, res, next) {

fixes the issue.

sigint
  • 1,842
  • 1
  • 22
  • 27