TypeScript Modules: ESM vs CommonJS and moduleResolution

ESM vs CommonJS in TypeScript, module and moduleResolution settings, file extensions in imports, and fixing "Cannot find module" errors.

In this chapter

Modules: ESM, CommonJS and resolution

More on Namespaces, modules and declarations

Keep learning

Overview

chapters · namespaces and declarations Modules: ESM, CommonJS and resolution Tell TypeScript who runs your code (Node or a bundler) and it will check imports the way that runtime resolves them.

1. The problem: tsc is happy, Node is not

Module resolution means: how an import path like './booking' is turned into a real file. tsc is the TypeScript compiler. A bundler (like Vite) is a tool that packs files and fixes paths. moduleResolution: bundler, run with node Handy when the same files also run directly as .ts: with Node's type stripping (Node deletes the types and runs the rest) or with Deno.

5. import type and verbatimModuleSyntax

Types vanish at compile time. The question is whether the import line vanishes too. That matters for side effects: code that runs just because a file is loaded, like a console.log at its top. value import of a type 'Booking' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. With the flag, every import line is kept in the JavaScript exactly as written. But Booking is only a type, and booking.js has no Booking export. So TypeScript makes you mark it with type. import type Only the name is erased. The module still loads, so its side effects still run. flag off In a CommonJS file, tsc turns each import into a require call. Before Node 22.12, require could not load an ES module, so TypeScript stops you. Move to ESM add "type": "module" (best long-term fix) Dynamic import await import('geo-lite') works from CJS Node 22.12+ & TS 5.8+ require(esm) is allowed under module nodenext (no top-level await in the ESM) Publishing both separate types per condition: index.d.mts for import, index.d.cts for require

package.json of a dual package

A dual package ships two builds: ESM for import, CJS for require. One shared .d.ts for both formats is a classic bug: the types describe only one of them. Check packages with arethetypeswrong, a free checking tool.

Remember

Bundler app: module: preserve + moduleResolution: bundler + noEmit. Node app or library: module: nodenext. Under nodenext, ESM imports need the output extension: ./booking.js. "type" in package.json and .mts/.cts decide ESM vs CJS per file. node10 ignores "exports": avoid it. ! Cheat code Match the config to the runtime: bundler → bundler, Node → nodenext. Write .js in Node ESM imports. verbatimModuleSyntax + import type make every import line honest. → Go deeper: declaration files and @types, tsconfig that catches bugs, inside the compiler (isolatedModules), monorepos and project references.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.