TypeScript Dual Module Builder

0.6.3 · active · verified Sun Apr 19

ts-dual-module is a command-line tool designed to simplify the creation of dual-module (ESM + CommonJS) packages using TypeScript. It targets projects that define `"type": "module"` in their `package.json` and automatically builds both module formats from TypeScript sources, typically located in a `./src` directory and outputting to `./dist`. The tool handles the complexities of `package.json` `exports` and `typesVersions` fields to ensure correct module resolution and type declarations for both environments, specifically addressing CommonJS subpath export issues where TypeScript's default `moduleResolution` might fall short. The current stable version is 0.6.3, indicating it's actively maintained but still in a pre-1.0 development phase, which implies a focus on adding features and refining functionality, potentially with breaking changes between minor versions. Its key differentiator is the automation of dual-module bundling and `package.json` configuration, saving developers from complex manual setup or reliance on multiple build steps with different TypeScript configurations.

Common errors

Warnings

Install

Imports

Quickstart

Sets up a new TypeScript project with `type: "module"`, installs `ts-dual-module`, initializes it, builds dual ESM/CJS outputs, and demonstrates usage.

mkdir my-dual-package
cd my-dual-package

npm init -y
# Add "type": "module" to package.json
node -e "const pkg = require('./package.json'); pkg.type = 'module'; require('fs').writeFileSync('./package.json', JSON.stringify(pkg, null, 2));"

npm install -D typescript ts-dual-module

mkdir src
echo 'export const greet = (name: string) => `Hello, ${name} from ESM!`;' > src/index.ts
echo 'export const farewell = (name: string) => `Goodbye, ${name} from CJS!`;' > src/cjs.ts

npx tsmod init # Follow prompts, e.g., default 'dist' output directory
npx tsmod build

# Verify output
cat dist/index.d.ts
cat dist/index.mjs
cat dist/index.js
cat package.json # Check for updated exports and typesVersions

node -e "import { greet } from './dist/index.mjs'; console.log(greet('ESM User'));"
node -e "require('./dist/index.js').greet ? console.log(require('./dist/index.js').greet('CJS User')) : console.log(require('./dist/index.js').farewell('CJS User - CJS path'));"

view raw JSON →