Node: Spread operator does not work without a flag --harmony

Created on 16 Nov 2015  路  18Comments  路  Source: nodejs/node

Spread operator does not work without a flag --harmony. Node.js version 5.0.0. In the documents it had supported https://nodejs.org/en/docs/es6/

example code:
'use strict';
let lazyRequireTask = (...args) => {
console.log(args);
}
lazyRequireTask('one', 'two', 'three');

Exception: SyntaxError: Unexpected token ...
at exports.runInThisContext (vm.js:53:16) ...

Most helpful comment

ok thanks

All 18 comments

your code is rest parameter. https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Functions_and_function_scope/rest_parameters
Spread operator is https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Spread_operator

If you would like to try rest params you put --harmony_rest_parameters in your execute options.

I want the code to work without flags, as written in the documentation node.js https://nodejs.org/en/docs/es6/

Which features ship with Node.js by default (no runtime flag required)?

Block scoping
let (strict mode only)
const
function-in-blocks (strict mode only [1])
Classes (strict mode only)
Collections
Map
WeakMap
Set
WeakSet
Typed arrays
Generators
Binary and Octal literals
Object literal extensions (shorthand properties and methods)
Promises
New String methods
Symbols
Template strings
Arrow Functions
new.target [2]
Object.assign
Spread operator [2]
You can view a more detailed list, including a comparison with other engines, on the compat-table project page.

your feature list is correct (you can use those features without flag).
And your list does not have rest params.
Rest params will be available in next or next of next major version.

ok thanks

The following also only works with the harmony flag, and seems to only use the spread operator. Think this is still an issue with v4.2.4

var parts = ['shoulders', 'knees'];
var lyrics = ['head', ...parts, 'and', 'toes']; // ["head", "shoulders", "knees", "and", "toes"]
console.log(lyrics);

gives the following output

node scratchpad
/repos/scraps/scratchpad.js:2
var lyrics = ['head', ...parts, 'and', 'toes']; // ["head", "shoulders", "knees", "and", "toes"]
                  ^^^

SyntaxError: Unexpected token ...
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:414:25)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:136:18)
    at node.js:963:3

however with the harmony flag it works as expected:

node --harmony scratchpad
[ 'head', 'shoulders', 'knees', 'and', 'toes' ]

If the above code is using another unsupported feature, would you be able to supply an example of the spread operator with runs without using the harmony flag please?

You need to use v5.x to get the spread operator.

@cjihrig When you say use v5.x what is the earliest x that this is available on?

Here's my setup

node --version
v5.10.1

node app
SyntaxError: Unexpected token ...

@denisbetsi you should likely update to the latest version of v6.x (v6.2.1)

@denisbetsi as pointed out, you should update. v5.x.x will be end of life soon. Your example works for me in v5.0.0 though:

$ node
> process.version;
'v5.0.0'
> var parts = ['shoulders', 'knees'];
undefined
> var lyrics = ['head', ...parts, 'and', 'toes'];
undefined
> console.log(lyrics);
[ 'head', 'shoulders', 'knees', 'and', 'toes' ]

Hello everyone, I am currently on node 6.9.1 and npm 3.10.8, and still getting the error of the rest param (ellipsis).

SyntaxError: Unexpected token ...

```javascript
let maxWeigth = Math.max(...opAreas.map(area => area.weight));
````

Your example works for me with v6.9.1. I'm going to speculate that you are actually running an older version. What does console.log(process.versions) print?

I updated it with nvm, and deployed via AWS CodeDeploy
When I try to run the code in the node command line, it works indeed.
If I try to run the app though, it shows the error.

Can any of this be an issue?

Edit:
I have this set on package.json

"engines": {
  "node": "6.9.1",
  "npm": "3.10.8"
},

Edit 2:
Answering your question @bnoordhuis

sudo node app.js
{ http_parser: '2.7.0',
  node: '4.6.2',
  v8: '4.5.103.42',
  uv: '1.9.1',
  zlib: '1.2.8',
  ares: '1.10.1-DEV',
  icu: '56.1',
  modules: '46',
  openssl: '1.0.2j' }
SyntaxError: Unexpected token ...

@bnoordhuis Do you know how is this possible?
I am running node -v and i get 6.9.1

node: '4.6.2', v8: '4.5.103.42'

You're using v4.x. sudo probably picks up the system-installed node, not the one you installed with nvm.

This solved it for me.

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo npm install pm2@latest -g
pm2 update

Thanks @bnoordhuis

Noteworthy:

Won't work:

node index.js --harmony

But this will work:

node --harmony index.js

Edit: Check out @TimothyGu 's comment below; v8 supports ES2015 fully.

@clusteratlas N.B.: We will not provide support for --harmony and many other V8 flags, so if you use them you are on your own. If I were you I'd just use v8.x, which supports ES2015 fully.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

addaleax picture addaleax  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments

Icemic picture Icemic  路  3Comments

akdor1154 picture akdor1154  路  3Comments

danielstaleiny picture danielstaleiny  路  3Comments