ES Modules in TypeScript
ES Modules in TypeScript — the core idea
ES Modules use import and export syntax standardized in JavaScript. Node.js, browsers, and bundlers may load modules differently, so the compiler's module setting must match the intended runtime. Module syntax is not identical to module resolution.
What you’ll learn
- Explain es modules in typescript with a concrete use case.
- Run the example and account for every line of its output.
- Recognize the difference between compile-time checking and runtime behavior.
Understand the rules and when to use them
Reading the code: Both exports are visible to another file, and are called normally inside this module. In an ESM project, a second file would import them with `import { square } from "./math.js"`. The .js specifier is intentional for emitted JavaScript.
Keep the boundary clear: Compiling to CommonJS and running as ESM (or the reverse) without aligning package.json and output settings can fail at runtime.
See the idea as a flow
Run the complete TypeScript example
Save the code as es-modules.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.
TypeScript code
export function square(value: number): number { return value * value; }
export const version = 1;
console.log(square(4));
console.log(version);Verified output
16
1Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable es-modules.ts; run: node es-modules.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.
Trace the example line by line
Both exports are visible to another file, and are called normally inside this module. In an ESM project, a second file would import them with import { square } from "./math.js". The .js specifier is intentional for emitted JavaScript.
The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.
Build the real multi-file example
This separate mini-project demonstrates real imports, exports, or declarations across files. Create each file in one folder, then compile and run main.js. The example was checked separately from the single-file program.
math.ts
export function square(n: number): number { return n * n; }
main.ts
import { square } from "./math.js";
console.log(square(6));
ESM setup: create package.json containing {"type":"module"}, use npx tsc --strict --target es2022 --module nodenext --moduleResolution nodenext main.ts, and run node main.js. Keep the package.json alongside the emitted JavaScript.
Verified output from the mini-project
36Avoid this common mistake
Compiling to CommonJS and running as ESM (or the reverse) without aligning package.json and output settings can fail at runtime.
Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.
Practice independently
Create two ESM source files and test them under NodeNext with package.json {"type":"module"}.
Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.
Check your understanding
What to remember
ES Modules use import and export syntax standardized in JavaScript.
Try the practice change and re-run the exact program to test your understanding.
Reaching the recap and completing the quizzes are separate milestones.