ProgramGuru

Introduction to Modules

Introduction to Modules — the core idea

A module is a file with its own scope whose exports form its public interface. In TypeScript, a top-level import or export makes a file an external module. A consumer must import exported bindings; unrelated top-level variables stay private to the module.

What you’ll learn

  • Explain introduction to 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 file contains an export, so it is a module. publicCode can be imported by another module. internalCode is still available within its defining file but not automatically available to other files.

Keep the boundary clear: A module does not need a class or namespace. Avoid assuming a top-level const in a module becomes global.

See the idea as a flow

Introduction to Modules — conceptual flowstatus.ts exports then main.ts imports then module scopeIntroduction to Modules1status.ts exports2main.ts imports3module scopeMatch 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 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

const internalCode = "hidden";
export const publicCode = "ready";
console.log(publicCode);
console.log(internalCode.length);

Verified output

ready
6

Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable modules.ts; run: node 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 file contains an export, so it is a module. publicCode can be imported by another module. internalCode is still available within its defining file but not automatically available to other files.

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.

status.ts

export const state = "ready";
const secret = 3;

main.ts

import { state } from "./status";
console.log(state);

CommonJS setup: compile with npx tsc --strict --target es2022 --module commonjs main.ts and run node main.js. Compile from the same directory so relative modules are discovered. If this example contains helpers.js, keep that runtime file alongside the emitted main.js.

Verified output from the mini-project

ready

Avoid this common mistake

A module does not need a class or namespace. Avoid assuming a top-level const in a module becomes global.

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

Split a program into status.ts exporting a status and main.ts importing it, then run the compiled entry point.

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 makes a TypeScript file a module?
Choose one answer. You can try again.

Answer and explanation

A top-level import or export.

The file contains an export, so it is a module. publicCode can be imported by another module. internalCode is still available within its defining file but not automatically available to other files.

What to remember

A module is a file with its own scope whose exports form its public interface.

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: ES Modules in TypeScript →