Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readTypeScript

Pick moduleResolution by what runs your code

Choose a resolution mode based on whether Node or a bundler resolves the imports you write.

Your project type-checks clean. You compile it, run node dist/main.js, and Node cannot find a file you import on line one.

// src/main.ts
import { x } from './util'
import { feature } from 'dep/feature'

console.log(x, feature())

For this example, set "type": "module" in package.json. In tsconfig.json, set module to esnext, moduleResolution to bundler, rootDir to src and outDir to dist. Export const x = 1 from src/util.ts.

For dep/feature, create a local package in dep/ and set "name": "dep". Give it "type": "module" and "exports": { "./feature": "./dist/feature.js" } in package.json. Export a feature function that returns 'ok' from dist/feature.js and declare export function feature(): string in dist/feature.d.ts.

Run pnpm add ./dep from the example project's root, then run pnpm exec tsc -p tsconfig.json. The emitted dist/main.js still says from './util'. Node's ES module loader requires an extension on relative file imports.

$ node dist/main.js
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '…/dist/util' imported from …/dist/main.js

TypeScript checked the import the way a bundler resolves it, and no bundler ever touched the output.

Pick the mode by what runs the output

moduleResolution tells tsc which resolver to imitate when it looks up an import. TypeScript 5.0 added bundler next to the Node modes, so the choice is now about who reads your compiled files.

diagram

What runs the output picks moduleResolution

Node and bundlers resolve imports differently. The node10 row shows the legacy option from the original example.

Use nodenext when Node resolves the imports. node16 models Node 16's fixed behavior. Use bundler when a bundler such as Vite or esbuild resolves the imports in your source.

What node16 asks for

Switch the same project to node16 to catch the missing extension before running the output.

$ pnpm exec tsc -p tsconfig.json --module node16 --moduleResolution node16
src/main.ts(2,19): error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './util.js'?

nodenext gave the identical error. The fix is to write './util.js' in the source even though the file on disk is util.ts.

tsc maps it to the TypeScript file, and the emitted import is one Node can load. After that change node dist/main.js printed 1 ok.

A folder import such as './lib' fails the same way, with TS2834, and needs './lib/index.js'. bundler accepts both folder and extensionless forms.

The ./util.js spelling also passes under bundler.

The exports map

node16, nodenext and bundler all read the exports map and resolve dep/feature.

node10 ignores exports, looks for a file named feature inside dep, and fails with Cannot find module 'dep/feature' or its corresponding type declarations. A modern package can type-check on three settings and break on the fourth.

July 2026 update. TypeScript 6.0 deprecated node10 and TypeScript 7.0 removed support. In a repo with a Vite app and a script that runs in Node, give each its own tsconfig.