Roman Pushkin Roman Pushkin - 21 days ago 11
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;

console.log(o1.aa);
console.log(o2.cc);


(purpose of the code above is to remove
aa
element from
o1
, store the rest in
o2
, without mutating
o1
).

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
/home/admin/experiments/ct/src/lru-cache.js:2
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
v6.9.1


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

Answer

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).