1

I'd like to destructure an array inside a function.

The following example passes an array as a single argument, then attempts to destructure the argument inside the function. Seems simple enough, but node throws an error.

// volume.js
function volume(box) {
  let [x,y,z] = box;
  return x * y * z;
}

volume([2,3,4]);

Running that with $ node volume.js (v5.1.1) returns this error:

./volume.js:2
  let [x,y,z] = box;
  ^

ReferenceError: let is not defined
    at volume (./volume.js:2:3)
    at Object.<anonymous> (./volume.js:5:1)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Function.Module.runMain (module.js:457:10)
    at startup (node.js:138:18)
    at node.js:974:3

There are other ways to solve this, but I don't understand why the above example doesn't work. Why does this throw an error? How can let be undefined?

joemaller
  • 19,579
  • 7
  • 67
  • 84
  • The syntax seems correct. Works in Firefox. Do you still need to set a flag in node to get ES6 features? –  Dec 06 '15 at 00:39
  • 2
    Based on [this answer](http://stackoverflow.com/a/28389178/1106925) from earlier this year (and edited last month), you need the `--harmony` flag for `let`. Are you using that flag? –  Dec 06 '15 at 00:40
  • The reason I say that is because of this page: https://nodejs.org/en/docs/es6/ I tried using `node --harmony` and also `node --harmony_destructuring` with the destructuring assignment and both gave the same error as you're getting using node 4.2.2. With babel, it compiles correctly however. – Shashank Dec 06 '15 at 00:51
  • Running with `--harmony` didn't change anything. Also, I don't believe we need the harmony flag anymore. Node/JS moves very fast, the v0.12 release mentioned in that answer is several major releases old by now. – joemaller Dec 06 '15 at 01:08
  • Actually @Shashank, that helped. Adding "use strict" (thought that was obsolete) and using the `--harmony_destructuring` flag worked. – joemaller Dec 06 '15 at 01:16
  • @joemaller cool I didn't know use strict was necessary to enable the feature so you have taught me something :) though I kind of wonder why it's necessary to do that lol. – Shashank Dec 06 '15 at 01:32
  • 1
    ps, once destructuring is available in node, you can write it as `function volume([x,y,z]) { return x * y * z; }` or even better `const volume = ([x,y,z]) => x * y * z;` – Mulan Dec 06 '15 at 09:35

1 Answers1

1

Destructuring doesn't work with vanilla node v5.1.x because destructuring is still considered an "in-progress" feature.

The let is not defined ReferenceError is raised because block scope declarations are currently limited to strict mode.

So, for the above code to work, node needs to be invoked with the harmony_destructuring flag and the code needs to be running in strict mode. Add "use strict" to the top of the file and run this:

$ node --harmony_destructuring ./volume.js

Node also offers a --use_strict flag, but I prefer to declare that per file.

The node.js documentation (current as of v5.1.1, as cited by @Shashank in comments), ES6/ES2015 features are divided up into three categories:

  • Shipping - enabled by default
  • Staged - almost ready, bulk-enabled with the --harmony flag.
  • In progress - experimental, can be individually enabled

Running node with the harmony flag, node --harmony only enables Staged features. In Progress features like destructuring need to be individually enabled. Here's the list of the in progress flags in node v.5.1.1:

  • --harmony_modules - harmony modules
  • --harmony_regexps - harmony regular expression extensions
  • --harmony_proxies - harmony proxies
  • --harmony_sloppy_function - harmony sloppy function block scoping
  • --harmony_sloppy_let - harmony let in sloppy mode
  • --harmony_unicode_regexps - harmony unicode regexps
  • --harmony_reflect - harmony Reflect API
  • --harmony_destructuring - harmony destructuring
  • --harmony_default_parameters - harmony default parameters
  • --harmony_sharedarraybuffer - harmony sharedarraybuffer
  • --harmony_atomics - harmony atomics
  • --harmony_simd - harmony simd
joemaller
  • 19,579
  • 7
  • 67
  • 84