Kelsadita
Kelsadita

Reputation: 1038

Nodejs import fails with syntax error

I am making use of react-router node module for routing in a react app. I am importing required modules as follows.

var React = require('react');
var Router = require('react-router');
var { Route, RouteHandler, Link } = Router;
// ... remaining code ...

But I am getting syntax error on line no. 3 i.e. var { Route, RouteHandler, Link } = Router;

Error:

Uncaught SyntaxError: Unexpected token {

Upvotes: 3

Views: 546

Answers (1)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276266

Doing:

var {x,y} = {x:3,y:5};

Is called a destructuring assignment and is a new feature in JavaScript, it requires a new JavaScript runtime. This feature is not supported in NodeJS yet and not even in v8 yet (the JS engine JavaScript runs on). You can either assign it in 3 lines manually or use a tool like Traceur or Babel to compile your ES6 (new specification of EcmaScript) to ES5 (what node runs) code.

Upvotes: 6

Related Questions