Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 {

like image 249
Kelsadita Avatar asked Feb 05 '26 05:02

Kelsadita


1 Answers

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.

like image 162
Benjamin Gruenbaum Avatar answered Feb 06 '26 18:02

Benjamin Gruenbaum