ProgramGuru

CommonJS vs ES Modules

CommonJS vs ES Modules — the core idea

CommonJS uses require/module.exports while ES Modules use import/export. TypeScript can emit either format; Node determines how JavaScript files are interpreted using extensions and package configuration. Choose one working combination rather than mixing formats accidentally.

What you’ll learn

  • Explain commonjs vs es modules 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: The same source can emit `exports.greeting = ...` in CommonJS or an export declaration in ESM. Its result does not depend on the emitted module syntax; module loading and package boundaries do.

Keep the boundary clear: Do not assume changing target changes the module system: --target and --module are separate compiler options.

See the idea as a flow

CommonJS vs ES Modules — conceptual flowTypeScript export then --module commonjs then --module es2022CommonJS vs ES Modules1TypeScript export2--module commonjs3--module es2022Match the compiler configuration to the JavaScript environment.
Follow the three stages and identify what happens in TypeScript versus JavaScript.

Run the complete TypeScript example

Save the code as commonjs-vs-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 greeting(name: string): string { return `Hello, ${name}`; }
console.log(greeting("Maya"));

Verified output

Hello, Maya

Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable commonjs-vs-es-modules.ts; run: node commonjs-vs-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

The same source can emit exports.greeting = ... in CommonJS or an export declaration in ESM. Its result does not depend on the emitted module syntax; module loading and package boundaries do.

The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.

Avoid this common mistake

Do not assume changing target changes the module system: --target and --module are separate compiler options.

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

Compile this source with --module commonjs and --module es2022, inspect the JavaScript differences, and run with compatible Node configuration.

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

Which compiler setting chooses the module output format?
Choose one answer. You can try again.

Answer and explanation

The --module compiler option.

The same source can emit `exports.greeting = ...` in CommonJS or an export declaration in ESM. Its result does not depend on the emitted module syntax; module loading and package boundaries do.

What to remember

CommonJS uses require/module.

Try the practice change and re-run the exact program to test your understanding.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Module Resolution →