Sub topic 4
How `nodenext` Resolves Imports
Assume you already know TypeScript import and export syntax, basic tsconfig.json, and that Node.js supports both CommonJS and ES modules. This chapter focuses on how TypeScript follows Node.js rules when it resolves an import.
Resolution follows the emitted module format
Module resolution is the process of turning an import specifier such as "./greet.js" or "date-fns" into a file. The compiler must answer two questions:
- Which file does this import refer to?
- Is the importing file CommonJS or an ES module?
With nodenext, TypeScript answers these questions using the same package and file rules that modern Node.js uses. The module option also selects the matching resolution behavior.
{
"compilerOptions": {
"module": "nodenext",
"target": "es2022",
"strict": true,
"outDir": "dist"
}
}TypeScript examines each file separately. A project can therefore contain both CommonJS and ES module files.
How Node.js decides a file's format
The file extension and the nearest package.json determine the format.
| File | Relevant setting | Format |
|---|---|---|
| `main.ts` in a package with `"type": "module"` | package type | ES module |
| `main.ts` in a package without `"type": "module"` | package type | CommonJS |
| `main.mts` | extension | ES module |
| `main.cts` | extension | CommonJS |
The .mts and .cts extensions override the package setting. TypeScript emits .mjs for .mts and .cjs for .cts. A normal .ts file follows the nearest package.json. “Nearest” means the first package file found while walking from the source file toward the project root.
Relative imports need runtime extensions
In an ES module file, Node.js requires a relative import to include its runtime extension. Write .js in the TypeScript source when the emitted file will be .js.
Suppose the package contains "type": "module" and these files:
src/
main.ts
greet.tsUse this import:
import { greet } from "./greet.js";
console.log(greet("world"));TypeScript resolves ./greet.js to the source file greet.ts while checking the project. After compilation, it becomes dist/greet.js, so the import remains valid at runtime.
Do not write this in an ES module file:
import { greet } from "./greet";That may appear convenient, but Node.js does not automatically add the missing extension for ES module imports. TypeScript reports an error because the emitted program would fail.
CommonJS files have different rules. Node.js CommonJS resolution can try extensions and directory entry points, so extensionless relative imports are often accepted there. The safest approach is still to test the emitted JavaScript, not only the TypeScript source.
Package imports and exports
For a package name, such as "example-lib", nodenext reads that package's package.json. Its exports field can choose a different entry point for an ES module import and a CommonJS require.
TypeScript uses the condition that matches the importing file's format. It also looks for declaration files so that the imported API has types. This means a package can publish separate JavaScript and declaration files for each format.
A practical checklist
When an import fails under nodenext, check these items:
- Identify whether the importing file is ESM or CommonJS.
- Check the nearest
package.jsonand its"type"field. - Use
.jsin relative imports from ESM source files. - Inspect the imported package's
exportsfield. - Confirm that the emitted JavaScript has the paths that Node.js will load.
Exercise
A package has "type": "module", and src/main.ts imports src/util.ts. What should the relative import specifier be, and why?
Show answer
Use ./util.js. TypeScript maps that specifier to util.ts during checking, while the emitted JavaScript file is util.js, which Node.js can load.