As title. Since Node.js now supports ES6 module, you can simply add "type": "module"
in your package.json
to facilitate the import
/export
syntax even without the --experimental-modules
flag to run your code via the node
CLI-command without any problem. To me:
require()
from CommonJS spec in our code.So why do some people want to use ES6 module? Is it only for importing modules asynchronously? I don't think so. Their most be some reasons more important than this. But apparently moving toward the newest syntax will be a trend. Any idea?
p.s. I've read some old threads saying that most testing frameworks haven't supported ES6 module syntax, and from the last point on the list of CommonJS:
It cannot be used to load ECMAScript modules (although it is possible to load ECMAScript modules from CommonJS modules).
It means (well, indeed not that readable at first look) that direct import of ES6 modules is NOT possible with CommonJS module but
you can do it indirectly. This could be one of the reasons most people don't bother to migrate to the newest import
syntax on Node.js.
Can anyone please correct me if some statement(s) I provided above is wrong?
The comment section under my OP has become quite big. So let me try to provide a conclusion for myself(and the future readers), after many tabs(probably over 100) now got closed after I (finally) had organized them.
What's the benefit of using ES6 module syntax in Node.js without transpilation?
The "without transpilation" means that the version of Node.js you're using has to support ES6 module, which it does since v12, as per @Felix Kling's answer.
[...] So it's actually NOT as simple as doing the same thing differently.
The point is that "You can reliably import
a CommonJS module in an ES module" (quoted from an article by Simon Plenderleith), where there is a link to the related pull request in Node.js repository. This is also mentioned by @jabaa under the comment section of my OP. But unfortunately, he/she doesn't have a reference for it.
So why do some people want to use ES6 module? Is it only for importing modules asynchronously? I don't think so. Their most be some reasons more important than this. [...]
One of those important reasons must be tree-shaking, which is mentioned twice in the comment section under my OP. And notice that while this might be possible on CommonJS modules, the performance of it with ES6 modules must be better, since the syntax-level ES6 import
is static in nature(I'm not talking about the import()
version), and ES6 modules will only be initialized once. With tree-shaking, your code gets smaller thus your website/library will run/load faster.
But apparently moving toward the newest syntax will be a trend. Any idea?
Absolutely. At the time I'm writing this, Node.js is default to "type": "commonjs"
because of compatibility, since during the old days ES6 modules syntax has not been merged/invented. Now using ES6 modules syntax is future-proof, it will work even when people will not use Node.js in the future(sorry about saying this! But apparently it will probably be replaced by something new/fancy in the future...)