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:
- This also implies that we actually don't need to transpile our ES6 module syntax to those
require()
from CommonJS spec in our code. - But in Node.js v16.x documentation, they explicitly list some differences between the specs of both CommonJS (module) and ECMAScript module (loaders). So it's actually NOT as simple as doing the same thing differently. There must be some trade-offs to prefer one over the other, and this is what I want to know!
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?