ES2015 import does not work in node v6.0.0 with the --harmony_modules option

I am using node v6.0.0 and wanted to use ES2016 (ES6). However, I realized that the "import" syntax does not work. Is β€œimporting” fundamental to writing modular code in ES2015? I tried to start node with the --harmony_modules , but still got the same error about "import". Here is the code.

Working code without "import":

 'use strict'; let sum = 0; class Number { addNumber(num1, num2) { return num1 + num2; } } let numberObj = new Number(); sum = numberObj.addNumber(1,2); console.log("sum of two number 1 and 2 "+ sum); 

Broken code with "import":

server.js

 'use strict'; import Number from "./Number"; let sum = 0; let numberObj = new Number(); sum = numberObj.addNumber(1,2); console.log("sum of two number 1 and 2 "+ sum); 

Number.js

 'use strict'; export default class Number { addNumber(num1, num2) { return num1 + num2; } } 

I also checked http://node.green/ to see es6 supported, but couldn't figure out why it doesn't work with the --harmony_modules option. Please, help.

+79
javascript ecmascript-6 es6-modules
Apr 27 '16 at 21:22
source share
4 answers

They are not yet implemented.

Node 6.0.0 uses version V8 with most of the features of ES6. Unfortunately, modules are not one of those complete functions.

 node --v8-options | grep harmony 

progress flags of harmony are not fully implemented and usually do not work:

- es_staging (enable properties compatible with test support ( internal use only ))
--harmony (enable all completed harmony functions)
--harmony_shipping (enable all sent harmony features)
--harmony_object_observe (enable "harmony Object.observe" ( in progress ))
- harmony_modules (enable "harmony modules" ( in progress ))
--harmony_function_sent (enable harmony function .sent ( in progress ))
--harmony_sharedarraybuffer (enable "harmony sharedarraybuffer" ( in progress ))
--harmony_simd (enable "harmony simd" ( in progress ))
--harmony_do_expressions (enable "expression of harmony" ( in progress ))
--harmony_iterator_close (enable "end of harmony iterator" ( in progress ))
--harmony_tailcalls (enable "harmony tail tails" ( in progress ))
--harmony_object_values_entries (enable "harmony Object.values ​​/Object.entries" ( in progress ))
--harmony_object_own_property_descriptors (enable "harmony Object.getOwnPropertyDescriptors ()" ( in progress ))
--harmony_regexp_property (enable "regexp property classes for Unicode matching" ( in progress ))
--harmony_function_name (include "expression of the harmony function name")
--harmony_regexp_lookbehind (enable "regexp lookbehind harmony")
--harmony_species (enable "harmony Symbol.species")
--harmony_instanceof (enable "harmony support")
--harmony_default_parameters (enable "harmony default parameters")
--harmony_destructuring_assignment (enable "assignment of destruction of harmony")
--harmony_destructuring_bind (enable "harmony destruction binding")
--harmony_tostring (enable "harmony toString")
--harmony_regexps (enable "harmony regex extensions")
--harmony_unicode_regexps (enable "unicode regexps negotiation")
--harmony_sloppy (enable "inaccurate harmony functions")
--harmony_sloppy_let (enable "sloppy harmony")
--harmony_sloppy_function (enable "reconcile the careless harmony function block")

--harmony_proxies (enable "harmony proxies")
--harmony_reflect (enable "Reflect Harmony API")
--harmony_regexp_subclass (include "subclass of regular expression of harmony")

+85
Apr 27 '16 at 9:31 on
source share

This should be a comment on @Paulpro's answer, but I do not have enough reviews to post a comment.

For Windows users , the equivalent command is:

 node --v8-options | findstr harmony 
+39
Jul 09 '16 at 16:28
source share

Until the modules are implemented, you can use the Babel transpiler to run your code:

 npm install --save babel-cli babel-preset-node6 ./node_modules/.bin/babel-node --presets node6 ./your_script.js 

See https://www.npmjs.com/package/babel-preset-node6 and https://babeljs.io/docs/usage/cli/

Downsides : this has several drawbacks, such as extra compilation time, which can be significant, and now you need the source maps for debugging; just saying.

+32
Oct 13 '16 at 9:36
source share

As stated above, ES6 modules are not yet implemented.

It seems like a non-trivial problem to implement ES6 modules in such a way as to be backwards compatible with Common JS modules, which is the current syntax of the Node.js module.

However, there is a project project that introduces a new file extension - .mjs - for files containing ES6 modules.

In addition, there is a counter proposal that presents an alternative approach to declaring all files with ES6 modules in package.json as follows:

 { "modules.root": "/path/to/es6/modules" } 
+16
Apr 28 '16 at 21:10
source share



All Articles