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

What problem does this solve?

Good Tutorials

Other Tools

Some random notes

  1. 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.

  1. What does tsconfig composite do?

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.

  1. What does tsconfig isolatedModules do?

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.

  1. 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.