5

I am using the latest nodejs version, but am still getting an error while using es6 module import.

I could also find on a blog that node doesn't yet support es6 import module. https://nodesource.com/blog/es-modules-and-node-js-hard-choices/ - no JavaScript runtime currently supports ES Modules. I am new to javascript and node, is anyone using es6 module import with nodejs, without transpiling the code to lower js versions.

3
  • It's not implemented, so you cannot "use" it. "without transpiling the code to lower js versions" --- in case of es2015 modules it's not "lower version", it's a "supported APIs" instead. Commented Dec 27, 2016 at 3:24
  • thanks, for confirming. please move this comment in answer section. Commented Dec 27, 2016 at 3:30
  • 2
    Possible duplicate of Using Node.js require vs. ES6 import/export Commented Dec 27, 2016 at 7:57

2 Answers 2

13

You can if you are willing to use a tool like Babel.

npm i -D babel babel-cli babel-core babel-preset-es2015 

Create a file .babelrc and put the following in it:

{ "presets": ["es2015"] } 

And then in package.json in the scripts section do some thing like this:

"dev": "babel src -d dist && node dist/your_file.js" 

This will transpile all of the code in a directory named src/ but it in a directory called dist/, and then run the file you specify.

It can then be used via the command as follows:

npm dev 
Sign up to request clarification or add additional context in comments.

1 Comment

Or use babel-node for running scripts
3

UPDATE 2019: import / export statements are supported in node v12. Right now behind the --experimental-modules flag. Which is supposed to be removed this October.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.