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.

mhatch
  • 4,441
  • 6
  • 36
  • 62
vashishth
  • 2,751
  • 4
  • 38
  • 68
  • 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. – zerkms Dec 27 '16 at 03:24
  • thanks, for confirming. please move this comment in answer section. – vashishth Dec 27 '16 at 03:30
  • 2
    Possible duplicate of [Using Node.js require vs. ES6 import/export](http://stackoverflow.com/questions/31354559/using-node-js-require-vs-es6-import-export) – Michał Perłakowski Dec 27 '16 at 07:57

2 Answers2

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
Matt Altepeter
  • 956
  • 1
  • 8
  • 18
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.

Gregor
  • 41
  • 1
  • 3