Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS (ES6): SyntaxError: Unexpected token {

let {name, description} = req.body;

NodeJS v5.2.0. Tried using flags - node bin/www --es_staging --harmony_destructuring. Throws an error: SyntaxError: Unexpected token {... Should I compile source using Babel, or is there any other way to use ES6 natively in NodeJS?

like image 365
Ramziddin Makhmudov Avatar asked Aug 29 '26 21:08

Ramziddin Makhmudov


1 Answers

Destructuring assigments are not supported by default in Node 5.2.0, but it will work if you enable it with node --harmony_destructuring.

However let is only supported in strict mode for now.
This is a V8 issue, and Node version 5.0.0, which will include V8 version 4.6 will also support let in sloppy mode.

Using let in sloppy mode at the moment, will throw the exact same error you're getting, so :

"use strict"

let {name, description} = {name: "some name", description : "some description"};

works fine in Node 5.2.0, note that the object, in your case req.body, must have the same keys as the variable names, otherwise they'll be undefined.

like image 112
adeneo Avatar answered Sep 01 '26 12:09

adeneo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!