admin管理员组

文章数量:1399172

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?

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?

Share Improve this question edited Mar 27, 2020 at 23:01 Alexander O'Mara 60.7k19 gold badges173 silver badges181 bronze badges asked Sep 26, 2017 at 4:48 Dan DascalescuDan Dascalescu 152k65 gold badges333 silver badges420 bronze badges 6
  • 1 Given that nothing else supports .mjs files yet, it seems like a good way to plicate things with minimal gains. – loganfsmyth Commented Sep 26, 2017 at 5:06
  • @loganfsmyth: yarn and npm "support" .mjs in that they don't care about the extension. The gain is the very reason why native module support has been worked on. – Dan Dascalescu Commented Sep 26, 2017 at 9:07
  • Yeah I should clarify. It's going to be awesome, I just feel like it's too early to start doing this. For instance, Node's layer for loading CommonJS from ES6 is different from Babel and Webpack, so code that works in one is not guaranteed to work in the other. Webpack also hasn't been set up to parse .mjs files in a spec-pliant way. Babel for instance allows module.exports and require usage in ES6 modules, and Webpack allows require. My is essentially that if you publish a module saying it supports native modules, people will expect it to work in those usecases too. – loganfsmyth Commented Sep 26, 2017 at 16:20
  • 1 Just an FYI: I've updated my answer with a way to continue to use the main entry. – Alexander O'Mara Commented Oct 27, 2017 at 23:23
  • 1 The documentation didn’t mention this case. It should be improved IMO. – Franklin Yu Commented Jan 26, 2018 at 15:09
 |  Show 1 more ment

1 Answer 1

Reset to default 12

This is possible with 13.7.0+ using conditional exports (which as of 13.10.0+ are no longer experimental). It's not well documented or obvious how to do this in a pletely backwards-patible way, but here's the trick which I previously researched back when it was experiemental:

node_modules/mod/package.json

{
    "main": "./lib.js",
    "exports": {
        ".": [
            {
                "import": "./lib.mjs",
                "require": "./lib.js",
                "default": "./lib.js"
            },
            "./lib.js"
        ]
    }
}

node_modules/mod/lib.js

exports.format = 'cjs';

node_modules/mod/lib.mjs

export const format = 'mjs';

Now it's possible to use both CommonJS:

main.js

const {format} = require('mod');

console.log(format);
$ node main.js
cjs

And ES Modules:

main.mjs

import {format} from 'mod';

console.log(format);
$ node main.mjs
mjs

Prior to this is was possible at one point to just use an extension-less main entry in package.json, but this feature was removed. See the revision history on this answer if interested.

本文标签: