15

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
piggyback
  • 9,034
  • 13
  • 51
  • 80

2 Answers2

6

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.

barry-johnson
  • 3,204
  • 1
  • 17
  • 19
  • 1
    you can use `babel` package from npm – langpavel Feb 19 '15 at 15:12
  • 2
    To get almost full compatibility with the ES6 standard you can install babel-node ```npm install -g babel-node``` and then run your code with babel-node. Here's [a link](https://babeljs.io/docs/setup/) in case this information becomes outdated. – Rakan Nimer Aug 05 '15 at 15:03
  • 1
    **Important**: the package name is `babel-cli`, **NOT** `babel-node`. – Jebediah Nov 01 '18 at 20:57
0

Try the --experimental-modules flag

Back in 2017, Node.js 8.9.0 shipped experimental support for ECMAScript modules, known for their import and export statements. This support was behind the flag --experimental-modules.

https://medium.com/@nodejs/announcing-a-new-experimental-modules-1be8d2d6c2ff

pinei
  • 2,233
  • 1
  • 24
  • 25