Model modern Node.js resolution
A project runs directly on current Node.js and may use both ESM imports and CommonJS require.
TypeScript · Const
moduleResolution chooses how TypeScript maps import, export, and require specifiers to files. Configure it to match the target runtime or bundler, such as nodenext for modern Node.js or bundler for bundler-managed projects.
moduleResolution selects the algorithm TypeScript uses to turn module specifiers from imports, exports, or require calls into files. Choose a mode that matches the runtime or bundler: node16/nodenext for modern Node.js, bundler for bundlers, and node10 only for older CommonJS environments.
"moduleResolution": "node16" | "nodenext" | "node10" | "bundler" | "classic"| Name | Type | Description |
|---|---|---|
moduleResolution | `node16` | `nodenext` | `node10` | `bundler` | `classic` | Resolution strategy used to locate files for module specifiers. Current documented choices include node16, nodenext, node10, bundler, and classic. |
No runtime return value; this compiler option changes how TypeScript resolves modules.
{
"compilerOptions": {
"module": "nodenext",
"moduleResolution": "nodenext",
"target": "es2022"
}
}Use Node's modern resolution rules for a Node.js project so TypeScript models the runtime's import and require behavior.
A project runs directly on current Node.js and may use both ESM imports and CommonJS require.
A web application is resolved and emitted by a bundler.
A legacy application targets an older Node.js CommonJS environment.
moduleResolution control?It determines how TypeScript resolves module specifiers to files. The setting should match the module-resolution behavior of the target runtime or bundler.
Follow-up: How would you choose it for a bundled web application versus a Node.js application? Use bundler for the former and nodenext or node16 for the latter, paired with the corresponding module setting.
Use node16 or nodenext for modern Node.js. These modes account for Node.js supporting both ECMAScript imports and CommonJS require, which use different resolution algorithms.
Follow-up: Why must the module option also matter? The emitted form determines whether Node.js applies import or require resolution, so the paired settings let TypeScript select the appropriate algorithm.
bundler differ from Node.js resolution?Use bundler when a bundler performs resolution. It supports package imports and exports while allowing extensionless relative imports, unlike Node.js resolution modes.
Follow-up: What compatibility issue can this create? Code accepted by bundler may fail when executed directly by Node.js because Node.js can require extensions on relative imports.
modulebaseUrlpathsresolveJsonModuleesModuleInterop