Dual build/publish CJS and ESM package
Created by Qiwei on 10/30/2023 · Edited on 9/7/2025
Notes about dual build/publish CJS and ESM package with
tsc
Examples Setups
- fragnova/blob/devel/js/api-augment: only tsc.
- vitest uses rollup.
What problem does this solve?
- For example, it fixes double import, also mentioned in the issue that there are a few ways to solve this Dual package hazard
Good Tutorials
Other Tools
Some random notes
- In monorepo, why package reference stopped working changing to dual bundle?
Solution is to remember change reference path in tsconfig to specific tsconfig file names, e.g. ../pkgA/tsconfig.esm.json or ../pkgB/tsconfig.cjs.json instead of ../pkgA.
- What does tsconfig
compositedo?
When “composite” is set to true in the base tsconfig, it enables incremental compilation and sets up the project to use project references. This means that TypeScript will generate an output directory with information about each project’s dependencies, which is used to speed up subsequent builds by only compiling what has changed.
- What does tsconfig
isolatedModulesdo?
When “isolatedModules” is set to true, TypeScript will not attempt to resolve module dependencies. This is useful when you are using a bundler like Webpack or Rollup to resolve modules at build time.
- Is it possible for someone to use esm build for server code?
Yes, it is possible for someone to use your ESM (ECMAScript module) build for server-side code.
ESM is a module system that is supported by many modern JavaScript runtimes, including Node.js. Node.js has implemented support for ESM starting from version 12. In ESM, you can use import and export statements to import and export modules.