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_moduleswalk-up, and the module-kind decision driven by the nearestpackage.json"type"field.node16andnodenextdiffer only in how they treat a fixed Node 16 versus evolving-next behavior;nodenextis the right default for anything Node actually executes, andnode20exists for pinning to Node 20's resolver.node10(formerly"node") andclassic— legacy.classicshouldn'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 runtimeThe 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.jsonpathsare compile-time aliases — Node never reads them. Apaths-mapped import that resolves fortsccan still 404 at runtime."exports"beats"main"/"types"for modern consumers; a package withmainbut noexportsresolves differently under bundler vs nodenext resolution.import x = require("x")andexport =are pre-ESM syntax on the erasable-syntax banned list; they'reimport/exportnow.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Building a Modern App With React, Vite, and Tailwind CSS: The Complete 2025 Guide
React for the UI, Vite for the build tooling, Tailwind for the styles. This is the stack most new frontend projects start with in 2025. This guide wires all three together from scratch and builds a real application structure you can actually start a production project from.
What is Vite? The Build Tool That Made Frontend Development Fast Again
Vite replaced Webpack for a lot of teams without much fanfare. Native ES modules, a dev server that starts in milliseconds, and a production build that doesn't require understanding 300 lines of config. Here's how it works and why the switch is worth it.
What is React? The JavaScript Library That Changed Frontend Development
React is a JavaScript library for building user interfaces. Facebook built it in 2013, open-sourced it, and the entire frontend ecosystem reorganized itself around it. Here is how the component model works, why it took over, and what you need to know before writing a single line.
Angular 18: Deferred Views & Built-in Control Flow
The newest release of Angular – which is Angular 18 – is planned to be released on May 22nd, 2024. This is Google's super popular TypeScript based webapp framework that allows developers to create amazing web applications. The features, changes, and goodies of this release are deferrable views…