最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I use es6 style module import in nodejs application - Stack Overflow

programmeradmin11浏览0评论

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. / - 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.

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.

Share Improve this question edited Sep 5, 2017 at 18:44 mhatch 4,6056 gold badges42 silver badges65 bronze badges asked Dec 27, 2016 at 3:23 vashishthvashishth 3,3704 gold badges44 silver badges71 bronze badges 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. – zerkms Commented Dec 27, 2016 at 3:24
  • thanks, for confirming. please move this comment in answer section. – vashishth Commented Dec 27, 2016 at 3:30
  • 2 Possible duplicate of Using Node.js require vs. ES6 import/export – Michał Perłakowski Commented Dec 27, 2016 at 7:57
Add a comment  | 

2 Answers 2

Reset to default 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

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.

发布评论

评论列表(0)

  1. 暂无评论