Roman Pushkin Roman Pushkin - 1 year ago 93
Javascript Question

ES6 destructuring assignment doesn't work in latest Node

I'm using the following code that works perfectly on es6fiddle:

const o1 = { aa: 11, bb: 22, cc: 33 };
let { aa, ...o2 } = o1;


(purpose of the code above is to remove
element from
, store the rest in
, without mutating

It works on web, but when I try to execute this code from my console it doesn't work. I tried with latest node (6.9.1 and 7.1.0), and it doesn't work. When I look at specs for these versions, seems like it should work.

Here is error message I got in my console:

$ node src/lru-cache.js
let { aa, ...o2 } = o1;
SyntaxError: Unexpected token ...
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3

$ node -v

What's wrong with my configuration and how can I make it work? Thanks!

Answer Source

The syntax you're using is called object rest properties. It isn't supported by Node.js yet; in fact, the only platform that currently supports it is Babel (see ES Next compatibility table).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download