Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: Unexpected token const { in node.js

I am a new Bie to node.js. I am getting Syntax Error:

Unexpected token { in my node.js application

I am using v4.5.5 on windows 7 64 bit os.

  const {
        ^

SyntaxError: Unexpected token {
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)

Let me know how to get rid of this.

Any help would be appreciated.

Thanks

Pradeep

like image 916
Pradeep Avatar asked Aug 09 '16 17:08

Pradeep


2 Answers

The issue got fixed by upgrading node js to v6.3.1 . After upgrading to 6.3.1 ,I got another error related to isomorphoc-fetch that I fixed it by using npm install --save isomorphic-fetch es6-promise

Ref Link: https://www.npmjs.com/package/isomorphic-fetch.

Thanks all for your inputs

like image 188
Pradeep Avatar answered Nov 18 '22 15:11

Pradeep


From your code const { name, age } = user; seems like you try using object destruction in Node v4 it's available in Node v6.

If it's not possible to switch to v6 you have to refactor the destruct statements into classical object property access(const name = user.name; const age = user.age). Also, you could transpile the code with babel.

like image 9
Risto Novik Avatar answered Nov 18 '22 15:11

Risto Novik