0

I'm running out of ideas. I was running node 6.5.0, but then I needed to write some code that has async/await. So I updated node version to 7.7.0 (verified by running node -v) and tried running my app.js again. Unfortunately I'm getting the same error.

Do you have any idea what is happening? I'm running the app with a command node app.js and node -v gives me v7.7.0 response.

I hope I included enough information, please let me know if there's anything I could add to help diagnose this.

Error:

async function navigateWebsite() {
    ^^^^^^^^
SyntaxError: Unexpected token function
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:528:28)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/lucas/app/myapp/app.js:178:19)
at Module._compile (module.js:556:32)`

I'm pasting this function into my app.js application - not sure how code helps if it's clearly an issue with node and not the code.

function test() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('test');
    }, 2000);
  });
}

async function navigateWebsite() {
  const msg = await test();
  console.log('Message:', msg);
}
LucasSeveryn
  • 5,984
  • 8
  • 38
  • 65
  • 1
    Can you share more code ? So that the actual problem can be debugged. Also can you share your babel configurations ? Checkout this [answer](https://stackoverflow.com/questions/41756622/using-async-await-in-node-7-4) as well. – Muhammad Zia Nov 14 '19 at 20:01
  • Feel free to share enough code base for a problem. – Rise Nov 15 '19 at 05:07
  • Agree. Enough information should have been provided regarding the code – Vipulw Nov 15 '19 at 11:59
  • I added the test code I'm pasting to check if async works. I dont use babel – LucasSeveryn Nov 15 '19 at 18:31
  • What gave you the impression that Node v7 supported async / await? – Patrick Roberts Nov 15 '19 at 18:36
  • feel free to use nvm and install LTS version so you will not face any such issues. – faizan baig Nov 15 '19 at 18:38
  • @PatrickRoberts see answer to this question: https://stackoverflow.com/questions/37815790/syntaxerror-unexpected-token-function-async-await-nodejs "Async functions are not supported by Node versions older than version 7.6." – LucasSeveryn Nov 15 '19 at 18:50
  • @LucasSeveryn nevermind, it seems that [Node v7.6+](https://node.green/#ES2017-features-async-functions) should not require a flag. – Patrick Roberts Nov 15 '19 at 19:02

0 Answers0