Up to Node v8.5.0, publishing a module written in ES6 to NPMJS was a straightforward process: transpile the ES6 code using a tool like Babel, and publish to NPMJS the resulting lib
directory, while your GitHub repo contains the src
files.
With v8.5.0, Node has released experimental support for native modules (export
/import
) via the --experimental-modules
flag. It is now possible to publish purely-ES6 modules to NPMJS, and use them without any transpilation, as long as the files involved have an .mjs extension.
How can I publish an ES6 module (.mjs) so that it can also be used with older Node versions, which don't support ES native modules?
.mjs
files yet, it seems like a good way to complicate things with minimal gains. – Tu.mjs
in that they don't care about the extension. The gain is the very reason why native module support has been worked on. – Councilwoman.mjs
files in a spec-compliant way. Babel for instance allowsmodule.exports
andrequire
usage in ES6 modules, and Webpack allowsrequire
. My is essentially that if you publish a module saying it supports native modules, people will expect it to work in those usecases too. – Tumain
entry. – Rustic