Skip to content

Lesson 11 of 14 · 22m

Module resolution: Node's resolver never reads tsconfig

bundler vs nodenext, the .js-extension rule on .ts imports, package.json "type", and the tsconfig Node itself recommends for strip-types.

Here is what happens when import { db } from "./db" compiles under moduleResolution: "bundler" and then runs under Node ESM: Node's resolver looks for a file literally named db — no extension probing, no db.js guess — and throws ERR_MODULE_NOT_FOUND. TypeScript's module resolution and Node's module resolution are different resolvers, and moduleResolution in tsconfig is you telling the compiler which resolver to simulate. Choose the wrong simulation and everything typechecks while nothing runs.

The options that matter

  • bundler — simulates what Vite/esbuild/webpack/Rollup actually do: extensionless imports, package.json "exports", path tricks. Correct for app code that always passes through a bundler.
  • node16 / nodenext — simulates Node's real resolver: full CJS/ESM rules, "exports" maps, node_modules walk-up, and the module-kind decision driven by the nearest package.json "type" field. node16 and nodenext differ only in how they treat a fixed Node 16 versus evolving-next behavior; nodenext is the right default for anything Node actually executes, and node20 exists for pinning to Node 20's resolver.
  • node10 (formerly "node") and classic — legacy. classic shouldn't appear in anything this decade.

The .js rule that confuses everyone

Under nodenext, ESM imports use emitted file names, because the emitted file is what the resolver sees:

import { db } from "./db.js"; // resolves db.ts at compile time, db.js at runtime

The specifier says .js; the file on disk is .ts. This is not a typo to fix — it's the contract. TS 5.7 added rewriteRelativeImportExtensions for the people who can't stand it: write ./db.ts, and tsc rewrites it to ./db.js on emit. Pick one convention per project; mixing them produces imports that resolve under one tool and fail under another.

Module kind is decided per file by the nearest package.json "type": "module" makes .ts/.js ESM and .cts/.cjs CJS; absent or "commonjs" flips it. A .ts file in a "type": "module" package is ESM with no flag anywhere — which is why your "exports" map, your "type", and your moduleResolution are one decision, not three.

What Node itself recommends

Node's own TypeScript docs (type stripping is stable since v24.12/v25.2, enabled by default since 22.18/23.6) recommend this config for code Node runs directly:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "nodenext",
    "rewriteRelativeImportExtensions": true,
    "erasableSyntaxOnly": true,
    "verbatimModuleSyntax": true
  }
}

verbatimModuleSyntax is the sleeper in that list. With it, import { Foo } emits the import exactly as written — no elision — and import type becomes mandatory for type-only imports. That matters because transpile-only tools can't tell import { Foo } used only as a type from import { Foo } used as a value; without import type, they'd emit a real import of a name that may not exist at runtime. erasableSyntaxOnly is in the enums lesson; rewriteRelativeImportExtensions covers the .ts-in-imports style.

Apps vs libraries

App behind a bundler: moduleResolution: "bundler", verbatimModuleSyntax, and erasableSyntaxOnly if you also run scripts under Node strip-types. Library published to npm: module: "nodenext", real .js extensions (or the rewrite flag), and ship .d.ts — isolatedDeclarations (TS 5.5) enforces the "every export has an explicit annotation" rule that lets fast tools emit declarations per file without full type inference, which is how the post-tsc toolchain world generates .d.ts at bundler speed.

Try this

Set module: "nodenext", moduleResolution: "nodenext" on a scratch package with "type": "module", and run a two-file project through tsc then node. Now remove .js from an import and watch which tool fails first. Then flip rewriteRelativeImportExtensions on, write .ts in the specifier, and re-emit.

Pitfalls

  • tsconfig.json paths are compile-time aliases — Node never reads them. A paths-mapped import that resolves for tsc can still 404 at runtime.
  • "exports" beats "main"/"types" for modern consumers; a package with main but no exports resolves differently under bundler vs nodenext resolution.
  • import x = require("x") and export = are pre-ESM syntax on the erasable-syntax banned list; they're import/export now.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.