Write the package in CommonJS or transpile ES module sources into CommonJS, and
create an ES module wrapper file that defines the named exports. Using
[Conditional exports][], the ES module wrapper is used for import
and the
CommonJS entry point for require
.
// ./node_modules/pkg/package.json
{
"type": "module",
"main": "./index.cjs",
"exports": {
"import": "./wrapper.mjs",
"require": "./index.cjs"
}
}
The preceding example uses explicit extensions .mjs
and .cjs
.
If your files use the .js
extension, "type": "module"
will cause such files
to be treated as ES modules, just as "type": "commonjs"
would cause them
to be treated as CommonJS.
See Enabling.
// ./node_modules/pkg/index.cjs
exports.name = 'value';
// ./node_modules/pkg/wrapper.mjs
import cjsModule from './index.cjs';
export const name = cjsModule.name;
In this example, the name
from import { name } from 'pkg'
is the same
singleton as the name
from const { name } = require('pkg')
. Therefore ===
returns true
when comparing the two name
s and the divergent specifier hazard
is avoided.
If the module is not simply a list of named exports, but rather contains a
unique function or object export like module.exports = function () { ... }
,
or if support in the wrapper for the import pkg from 'pkg'
pattern is desired,
then the wrapper would instead be written to export the default optionally
along with any named exports as well:
import cjsModule from './index.cjs';
export const name = cjsModule.name;
export default cjsModule;
This approach is appropriate for any of the following use cases:
- The package is currently written in CommonJS and the author would prefer not to refactor it into ES module syntax, but wishes to provide named exports for ES module consumers.
- The package has other packages that depend on it, and the end user might
install both this package and those other packages. For example a
utilities
package is used directly in an application, and autilities-plus
package adds a few more functions toutilities
. Because the wrapper exports underlying CommonJS files, it doesn’t matter ifutilities-plus
is written in CommonJS or ES module syntax; it will work either way. - The package stores internal state, and the package author would prefer not to refactor the package to isolate its state management. See the next section.
A variant of this approach not requiring conditional exports for consumers could
be to add an export, e.g. "./module"
, to point to an all-ES module-syntax
version of the package. This could be used via import 'pkg/module'
by users
who are certain that the CommonJS version will not be loaded anywhere in the
application, such as by dependencies; or if the CommonJS version can be loaded
but doesn’t affect the ES module version (for example, because the package is
stateless):
// ./node_modules/pkg/package.json
{
"type": "module",
"main": "./index.cjs",
"exports": {
".": "./index.cjs",
"./module": "./wrapper.mjs"
}
}