NodeJS harmony gives SyntaxError on import

piggyback picture piggyback · Apr 14, 2014 · Viewed 10.2k times · Source

I was testing node with ES6 with the flag child_process --harmony but it fails at first step when I import. Any ideas?

import {'spawn'} from child_process;
console.log(spawn);

And i run:

node --harmony test.js

And I get:

:1
(function (exports, require, module, __filename, __dirname) { import {spawn} f
                                                              ^^^^^^
SyntaxError: Unexpected token import

Answer

barry-johnson picture barry-johnson · Apr 14, 2014

Not all features of ES6 are supported in v8 and thus in Node. Modules I believe are one of those things.

You might find this compatibility matrix handy: http://kangax.github.io/es5-compat-table/es6/

You could use the Traceur compiler to do this, although not everything is implemented with modules yet it appears

Edit in Dec 2015 - as some comments to this answer suggest, at this point, Babel has become the preferred ES6 transpiler. At the time I wrote my original answer, Babel was still known as 6to5 and was not as popular as Traceur. Today, Babel is the go-to choice.