ES2015 "import" not working in node v6.0.0 with with --harmony_modules option
Solution 1
They're just not implemented yet.
Node 6.0.0 uses a version of V8 with most of ES6 features completed. Unfortunately modules isn't one of those completed features.
node --v8-options | grep harmony
in progress harmony flags are not fully implemented and usually are not working:
--es_staging (enable test-worthy harmony features (for internal use only))
--harmony (enable all completed harmony features)
--harmony_shipping (enable all shipped 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 "harmony do-expressions" (in progress))
--harmony_iterator_close (enable "harmony iterator finalization" (in progress))
--harmony_tailcalls (enable "harmony tail calls" (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 "harmony unicode regexp property classes" (in progress))
--harmony_function_name (enable "harmony Function name inference")
--harmony_regexp_lookbehind (enable "harmony regexp lookbehind")
--harmony_species (enable "harmony Symbol.species")
--harmony_instanceof (enable "harmony instanceof support")
--harmony_default_parameters (enable "harmony default parameters")
--harmony_destructuring_assignment (enable "harmony destructuring assignment")
--harmony_destructuring_bind (enable "harmony destructuring bind")
--harmony_tostring (enable "harmony toString")
--harmony_regexps (enable "harmony regular expression extensions")
--harmony_unicode_regexps (enable "harmony unicode regexps")
--harmony_sloppy (enable "harmony features in sloppy mode")
--harmony_sloppy_let (enable "harmony let in sloppy mode")
--harmony_sloppy_function (enable "harmony sloppy function block scoping")
--harmony_proxies (enable "harmony proxies")
--harmony_reflect (enable "harmony Reflect API")
--harmony_regexp_subclass (enable "harmony regexp subclassing")
Solution 2
This should be a comment to @Paulpro's answer but I do not have enough rep to post a comment.
For Windows users the equivalent command is:
node --v8-options | findstr harmony
Solution 3
Until 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 various downsides, such as extra compilation time, which can be significant and you now need source maps for debugging; just saying.
Solution 4
As is stated above, ES6 modules are not implemented yet.
It appears to be a non-trivial issue to implement ES6 modules in a way that would be backward-compatible with Common JS modules, which is the current Node.js module syntax.
However, there is a draft of an implementation, that introduces a new file extension - .mjs
- for a files containing ES6 modules.
Also, there is a counter-proposal that present an alternative approach of declaring all files with ES6 modules in package.json like so:
{
"modules.root": "/path/to/es6/modules"
}
Related videos on Youtube
joy
I am Senior Software engineer. These days I am mainly working as full stack engineer. Which includes frontend, backend, unit test, end to end test automation, mongodb etc. I mainly working on reactjs, react-native, nodejs, mongodb, EC2 instances.
Updated on July 05, 2022Comments
-
joy almost 2 years
I am using node v6.0.0 and wanted to use ES2016 (ES6). However I realized that the "import" syntax is not working. Isn't "import" fundamental to for writing modular code in ES2015? I tried running node with
--harmony_modules
option as well but still got the same error about "import". Here's 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);
Notworking 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 the supported es6 however not able to understand why it doesn't work with --harmony_modules option. Please help.
-
Naramsim almost 8 yearsuse Webpack together Babel loader
-
chovy over 7 yearsYou should not need babel or webpack in v6
-
Dan Dascalescu over 7 years
-
jakub.g over 7 yearsrelated SO question: NodeJS plans to support import/export es6 (es2015) modules
-
jakub.g over 7 yearsSee also this blog post from Sep 2016 explaining challenges in implementing ES6
import
in node. It seems we're still a long way before ES6 modules ship in node.
-
-
joy about 8 yearsThanks. I saw it but was not believing myself as "import" is one of the important syntax of es6
-
Paul about 8 years@joy Yeah, hopefully it's available soon.
-
KingWu about 8 yearsThanks. where can i find the in progress list?
-
Paul about 8 years@KingWu I just found it by running
node --v8-options | grep harmony
after updating tonode 6.0.0
. -
Mr. Goferito over 7 yearsIs there any place where we can check the plans to integrate this option? Maybe for node 7?
-
Paul Everitt over 7 yearsHere is a relevant bug to watch: bugs.chromium.org/p/v8/issues/detail?id=1569
-
Paul Everitt over 7 yearsI think this is an important point. "The Powers That Be" are still arguing about the spec, and then it has to appear in v8 before it can get to Node.
-
Matthew Dean over 7 yearsJust a note: The newest draft does not require
.mjs
as an extension, which is much better for compatibility. -
Kokodoko over 7 yearsI was so glad to hear node 6 was out, and now they didn't implement one of the most important features ... :(
-
Alejandro B. over 7 years@Mr.Goferito et all, you can see what is and what isn't implemented here: node.green
-
Ali Gajani over 7 years@SuperUberDuper Use Node 7
-
Pawel almost 7 years@AliGajani I've got node v8.1.3 and still the same problem.
-
Marc Brillault over 6 yearsQuestion is : where can I find these es6 modules to download ?
-
Роман Парадеев over 6 yearsThis blog post may be a good place to start.
-
jdunning over 6 yearsThis article from Feb 10 2017 says it'll be at least a year: medium.com/the-node-js-collection/… So maybe mid-2018?