Skip to lesson

TypeScript · Lesson 33 of 42

Modules, imports and exports in TypeScript

Split code across files with import and export, use import type for types, understand default exports, and see how module and moduleResolution affect imports.

Beginner to advanced·15 min·Modules, async code and tooling·Updated 2026-11-24

UNDERSTAND IT. THEN TRY IT.

What you can do after this lesson

You will organise a TypeScript project across many files, sharing both code and types, and you will understand the compiler options that decide how those files find each other. Real programs are never a single file, so this lesson connects everything so far to how you actually work.

Files as modules

A file becomes a module when it has a top-level import or export. Its names are then private to the file unless exported. A file with neither is a script, and its top-level names are global, which is a common source of confusing "duplicate identifier" errors. Adding export {} to a file turns it into a module without exporting anything.

A small project with two files looks like this. The first file exports a function and a type:

typescript
// geometry.ts
export type Point = { x: number; y: number };

export function distance(a: Point, b: Point): number {
  return Math.hypot(b.x - a.x, b.y - a.y);
}

const secret = 42; // not exported: private to this file

The second file imports them by name:

typescript
// main.ts
import { distance, type Point } from "./geometry";

const a: Point = { x: 0, y: 0 };
const b: Point = { x: 3, y: 4 };
console.log(distance(a, b));

Running this program prints 5. Trying to import secret, which was not exported, is a compile error, because the module never shared it.

Importing types

Types do not exist at run time, so importing a type should leave no trace in the output. You can make that explicit with the type keyword:

typescript
// Form 1: the whole import is type-only
import type { Point } from "./geometry";

// Form 2 (use instead of form 1): an inline type-only name
import { distance, type Point } from "./geometry";

Both forms are completely removed from the JavaScript. Marking type imports has two benefits: readers can see what is a type and what is a value, and tools that compile one file at a time can safely remove the import without knowing what the other file contains. The verbatimModuleSyntax option makes the compiler require this style, which many projects enable.

You can also export a type on its own line:

typescript
export type { Point };

Default exports

Besides named exports, a module can have one default export:

typescript
// logger.ts
export default function log(message: string): void {
  console.log(message);
}

// main.ts
import log from "./logger";
import writeLog from "./logger"; // also fine: any name works

The importing file may call it anything. That flexibility is the reason many teams prefer named exports: the name is the same in every file, search finds every use, and an automatic rename updates all of them. Default exports are common for framework conventions, such as page components, so you will still meet them.

Re-exporting

A file can gather exports from others, a common pattern for an index.ts that forms a folder's public face:

typescript
// shapes/index.ts
export { Circle } from "./circle";
export { Square } from "./square";
export type { Shape } from "./shape";

Users then write import { Circle, Square } from "./shapes" and need not know the internal layout.

How import paths are found

When the compiler sees from "./geometry", it must decide what file that means. The moduleResolution option controls the rules, and there are two practical styles:

  • bundler resolution is designed for projects where a bundler reads your code. It lets you omit the extension, so "./geometry" finds geometry.ts.
  • node16 / nodenext resolution follows what real Node.js does. In a file that Node treats as an ES module, a relative import must include a file extension. You write "./geometry.js" even though the source file is geometry.ts, because the import refers to the file that will exist after compilation. A file that Node treats as CommonJS may omit the extension.

Which of the two a file is depends on the nearest package.json: with "type": "module" your .ts files are ES modules, and otherwise they are CommonJS. (Older compiler versions also offered a node10 setting that allowed extensionless paths. It is deprecated and has been removed from the newest versions.)

The extension rule surprises everyone once. It is correct, and it is why the compiler asks for it: the path in your source is exactly the path that Node will look up at run time.

Bare names such as "zod" or "react" refer to packages in node_modules, and their types come from the package itself or from an @types package, which a later lesson covers.

The module option

The module setting chooses the format of the emitted JavaScript:

  • ES modules (import and export stay as they are) for modern Node.js and for bundlers.
  • CommonJS (require and module.exports) for older Node.js code.

Pick the format your runtime or bundler expects. If you use a tool such as a bundler, it usually reads your TypeScript itself, and tsc is used only to check types (noEmit). If you ship plain tsc output to Node.js, module: "nodenext" is the setting designed for it. The lesson after this one shows the complete configuration.

Circular imports

If a.ts imports b.ts and b.ts imports a.ts, you have a circular import. It can work, but values may be undefined at the moment one file reads them, producing confusing errors. Break the cycle by moving shared code into a third file that both import.

Common mistakes

Leaving a file with no import or export. Its top-level names become global and can clash with other files. Add export {} or a real export.

Omitting the extension under nodenext. In files that are ES modules, relative imports need the extension, written as .js.

Importing types as values. It often works, but import type states your intent and is safer with single-file compilers.

Creating circular imports. Move shared code to a third module.

Deep imports that reach into another package's folders. Import only from a module's public entry point.

Exercise

Create three files: types.ts exporting a Task type, store.ts exporting addTask and listTasks functions that use it, and main.ts that imports from both and prints the list. Use import type for the type. Then try importing something you did not export, read the error, and add a re-exporting index.ts.

Next: tsconfig.json and strict mode.

KNOWLEDGE CHECK

A quick check. You’ve got this.

Try each question. Every answer comes with an explanation.

0 / 4 understood
01 What turns a TypeScript file into a module?
02 What does import type { User } from "./user" do at run time?
03 Why do many teams prefer named exports to default exports?
04 What does the module option control?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Frontend

TypeScript tutorial

Add types to JavaScript to catch mistakes before your code runs, from your first annotation to generics and real projects.

Your progress 0/42
Learning something new?Explore all courses