Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js application SyntaxError: Unexpected reserved word 'class'

I am new to Node.js and just started building an application using Eclipse and Nodeclipse plug-ins. When I run the app using Node core.js it works fine, but using Eclipse it shows me the following error:

/home/sarojs/projects/Core/security.js:3
  class Security {
  ^^^^^
SyntaxError: Unexpected reserved word
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/home/sarojs/projects/node/liveprojects/ecurvInc/Core/core.js:12:16)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)

I can't figure out what is wrong here. I googled for the solution and found some resources Unexpected reserved word, but I didn't understand how to incorporate it into Eclipse.

My package.json looks like this:

{
 "dependencies": {
 "bluebird": "^3.4.0",
 "body-parser": "~1.8.1",
 "cookie-parser": "~1.3.3",
 "cors": "^2.8.1",
 "debug": "~2.0.0",
 "express": "~4.9.0",
 "express-handlebars": "^3.0.0",
 "express-session": "^1.13.0",
 "mysql": "^2.11.1",
 "passport": "^0.3.2",
 "passport-local": "^1.0.0"
 }
}
  • Node.js version: 4.4.6
  • IDE: Eclipse Mars 1 with Nodeclipse plug-ins

Any help would be highly appreciated.

like image 667
Saroj Avatar asked Nov 23 '16 18:11

Saroj


3 Answers

The class declaration is an ES6 Feature. ES6 features are hardly supported in node 4.4.6. Try updating it and running npm install again

Also see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class

like image 115
Tom M Avatar answered Oct 14 '22 08:10

Tom M


I solved the problem after scratching my head for so many hours.

As @Tom M said the class declaration is an ES6 feature and previously i had node 4.2.6 but when i updated it to v4.6.2 the error vanished.

I downloaded the binary from here and install the node by extracting the file manually to my /usr/local directory and then ran

 npm install

You can find how to install node binary manually here

like image 2
Saroj Avatar answered Oct 14 '22 09:10

Saroj


JavaScript classes were recently introduced in ECMAScript 6. They are syntactic sugar over the previous prototypical inheritance model.

You can check which version of node supports class syntax over node.green

You can see the class concept is supported from v4.6.2 so try to update it to at least 4.6.2.But not all the ES6 feature are supported by this version.

like image 2
Abstract View Avatar answered Oct 14 '22 09:10

Abstract View