Description
openedon Sep 13, 2017
Experimental support for ES modules just landed in Node 8.5 (changelog, PR).
Since ES modules have some parsing and semantic differences, Node decided to use the mjs
extension for ES modules (while js
is for the "script" target and commonjs modules).
The current Typescript version (2.5.2
) supports ES modules emission but uses the js
extension by default. It means that to use it with Node, a post-compilation step is required to change the extension from js
to mjs
. This adds undesirable complexity to use native ES modules support with Node.
A solution would be to add a compiler option to output *.mjs
files when emitting ES modules.
Edit (2018-03-22): The propositions below are a bit outdated. I recommend reading the issue to see the progression. See this comment for my current proposition.
Notes:
- It is possible that new parse goals will be added in the next versions of the ES spec. Letting the user provide the extension explicitly (as opposed to a boolean switch) would allow to be forward compatible with other new extension.
- It should still be possible to emit ES modules with the
*.js
extension, many tools rely on thejs
extension. - Another solution would be to have a different extension for the source files:
*.mts
files would compile to*.mjs
, this would be similar to*.tsx
and*.jsx
.