Dual package compatibility
Concept and boundary
Dual packages need matching declarations for ESM and CommonJS entry points. NodeNext chooses import or require conditions according to the consuming file format. This fixture compiles both consumers and executes the ESM one; the CJS one is also checked for its distinct literal type.
Accepted project
Create a fresh folder with exactly these files. Use TypeScript 5.8.3 and Node.js 22.16.0. Package stubs under node_modules are local teaching fixtures, not installed dependencies.
import {mode} from "dual"; const m:"cjs"=mode; console.log(m);import {mode} from "dual"; const m:"esm"=mode; console.log(m);node_modules/dual/package.json
{ "name": "dual", "exports": { ".": { "import": { "types": "./esm.d.mts", "default": "./esm.mjs" }, "require": { "types": "./cjs.d.cts", "default": "./cjs.cjs" } } }}tsconfig.json
{ "compilerOptions": { "strict": true, "target": "ES2022", "module": "NodeNext", "moduleResolution": "NodeNext", "moduleDetection": "force", "skipLibCheck": true, "noEmitOnError": true, "types": [] }, "include": [ "**/*" ], "exclude": [ "node_modules", "dist", "build", "cache" ]}From the project root run: tsc -p tsconfig.json --pretty false.
Compilation succeeds (exit 0).
Run node consumer.mjs. Expected stdout:
esmRejected project — diagnosis
Intentionally rejected variant. Do not execute it.
Use a fresh copy of the accepted project, changing only the files shown below.
From the project root run: tsc -p tsconfig.json --pretty false.
Expected compiler diagnostic: TS2322
No runtime execution is required for this check.
A CommonJS consumer resolves the require declaration, not the import declaration. Restore the accepted configuration/source to compare the result.
Independent comparison: commonjs-consumer
Run the CommonJS consumer separately to verify that the require condition selects its runtime implementation.
Create a fresh folder with exactly these files. Use TypeScript 5.8.3 and Node.js 22.16.0. Package stubs under node_modules are local teaching fixtures, not installed dependencies.
import {mode} from "dual"; const m:"cjs"=mode; console.log(m);import {mode} from "dual"; const m:"esm"=mode; console.log(m);node_modules/dual/package.json
{ "name": "dual", "exports": { ".": { "import": { "types": "./esm.d.mts", "default": "./esm.mjs" }, "require": { "types": "./cjs.d.cts", "default": "./cjs.cjs" } } }}tsconfig.json
{ "compilerOptions": { "strict": true, "target": "ES2022", "module": "NodeNext", "moduleResolution": "NodeNext", "moduleDetection": "force", "skipLibCheck": true, "noEmitOnError": true, "types": [] }, "include": [ "**/*" ], "exclude": [ "node_modules", "dist", "build", "cache" ]}From the project root run: tsc -p tsconfig.json --pretty false.
Compilation succeeds (exit 0).
Run node consumer.cjs. Expected stdout:
cjsPractice and recap
Execute node consumer.cjs separately and compare the result.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dual package compatibility — knowledge check
Check the concept, interpret the tested project result, and diagnose a rejected configuration or source.
Which statement correctly describes Dual package compatibility?
Using the complete accepted project and the exact command in the lesson, which result is expected?
Why does the rejected project fail under its stated configuration?
What is the result of the independent commonjs-consumer comparison?