0

According to this compatibility table, there should be no problem with destructuring or the spread operator in Node 6.5

However, this code here:

const things = {
  a: 1,
  b: 2,
  c: true,
  d: false
};

const { a, b, ...rest } = things;

console.log(a); // 1
console.log(b); // 2
console.log(rest); // { c: true, d: false }

Throws this error:

const { a, b, ...rest } = things;
              ^^^
SyntaxError: Unexpected token ...
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:528:28)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

Even though in this babel compiler it works no problem.

Any ideas what's going on? (yes, I have Node 6.5 installed)

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Jona
  • 1,023
  • 2
  • 15
  • 39
  • You're trying to use the experimental object rest syntax. The official rest syntax only works on arrays. – Bergi Sep 12 '16 at 12:21
  • You even call it `rest`, why do you think this is spread syntax? –  Sep 12 '16 at 12:23
  • @ftor because i'm using the spread operator - the variable 'rest' is irrelevant, i could call it 'dog'. – Jona Sep 12 '16 at 12:26
  • @Bergi is there documentation anywhere on what counts as 'experimental' features? – Jona Sep 12 '16 at 12:26
  • @Jona No, the naming isn't irrelevant. Actually it describes your intention pretty well. –  Sep 12 '16 at 12:29
  • 1
    @Jona: Don't enable any of the `stage-N` presets and you don't get experimental features. http://babeljs.io/docs/plugins/#stage-x-experimental-presets – Bergi Sep 12 '16 at 12:30
  • @Bergi why on earth do rest and spread use the same syntax? –  Sep 12 '16 at 12:31
  • 1
    @ftor why on earth do destructuring and initialisers use the same syntax? Because convenience. – Bergi Sep 12 '16 at 12:32

0 Answers0