Summary
CommonJS and ESM are not compatible with each other. To produce both, your package needs to contain two versions of the code. To support usage in TypeScript, you’ll also need to include type definitions. The following sections describe how to set up the project to produce such output.
Node.js uses the.mjs extension to distinguish between CJS and ESM code. You need to rename the.js and.mj files to make it easier to build the package in both formats. You can also use the exports property to specify how the package should be loaded.
When building the ESM package, the helper script updates the extension to.mjs. This is required for the E SM build to work correctly. To support CJS and ESM, you need to produce two versions of the code, one for each module system.