There comes a time when you have to dual publish ESM (ECMAScript) and CJS (CommonJS) for your TypeScript projects. This guide should walk you through the steps to get this working properly with tsdown.
NOTE: if you are using tsup there is a codemod provided by tsdown to migrate.
tsdown setup
First let's install tsdown
with your favorite package manager, in this case I'll be using pnpm.
pnpm i -D tsdown
Now we can create the tsdown.config.ts
file that will have our configuration.
import { defineConfig } from "tsdown";
export default defineConfig([
{
// NOTE: this is the default but update if your entrypoint differs
entry: "src/index.ts",
format: ["esm", "cjs"],
sourcemap: true,
}
]);
package json setup
It's important to get these right otherwise types will fail to resolve in every scenario.
Add the following to your package.json
file.
{
// ...
"files": [
"dist"
],
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
}
// ...
}
validation
We can us the are the types wrong CLI to interrogate the output and ensure type resolution is working in all scenarios.
npx -y @arethetypeswrong/cli --pack .
Which should yield something like this if all goes well.
No problems found π
βββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ
β β "tsdown-dual-publish-esm-cjs" β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β node10 β π’ β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β node16 (from CJS) β π’ (CJS) β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β node16 (from ESM) β π’ (ESM) β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β bundler β π’ β
βββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββ
Demo
Here is a simple demo I've created to show what we've seen here.
tsdown-dual-publish-esm-cjs
If you want to migrate from tsup
this is a repo to follow.
pnpm i
pnpm test
What the ouput of test
looks like.
> tsdown
βΉ tsdown v0.14.2 powered by rolldown v1.0.0-beta.34
βΉ Using tsdown config: /Users/hacksore/code/tsdown-dual-publish-esm-cjs/tsdown.config.ts
βΉ entry: src/index.ts
βΉ tsconfig: tsconfig.json
βΉ Build start
βΉ Cleaning 4 files
βΉ [CJS] dist/index.cjs 0.10 kB β gzip: 0.11 kB
βΉ [CJS] 1 files, total: 0.10 kB
βΉ [CJS] dist/index.d.cts 0.09 kB β gzip: 0.10 kB
βΉ [CJS] 1 files, total: 0.09 kB
βΉ [ESM] dist/index.js 0.10 kB β gzip: 0.11 kB
βΉ [ESM] dist/index.d.ts 0.09 kB β gzip: 0.10 kB
βΉ [ESM] 2 files, total: 0.19 kB
β Build complete in 575ms
tsdown-dual-publish-esm-cjs v0.0.0
Build tools:
- typescript@~5.8.3
- tsdown@^0.14.2
No problems found π
βββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ
β β "tsdown-dual-publish-esm-cjs" β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β node10 β π’ β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β node16 (from CJS) β π’ (CJS) β
βββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
β node16 (from
β¦
Top comments (0)