Sub topic 5
Extensions in relative imports
Prerequisites and the core rule
Assume you are comfortable with TypeScript import and export statements, tsconfig.json, and running tsc. You should also know that Node.js supports both CommonJS and ES modules.
A relative import starts with ./ or ../, such as ./greet.js. Under nodenext, the required extension depends on the module format of the importing file.
For an ES module, Node.js requires the extension at runtime. TypeScript follows that rule during type checking. Write the extension of the emitted JavaScript file, not the extension of the TypeScript source file.
// src/main.ts
// package.json contains: { "type": "module" }
import { greet } from "./greet.js";
console.log(greet("world"));The source file is src/greet.ts, but the import uses ./greet.js. After compilation, the files are:
dist/main.jsdist/greet.js
The import therefore works when Node.js runs dist/main.js.
How nodenext decides what is allowed
With module set to nodenext, TypeScript examines the file extension and nearby package.json files. It uses those details to determine whether a file is an ES module or a CommonJS module.
| Source file | Typical format | Relative import rule |
|---|---|---|
| `.ts` inside a package with `"type": "module"` | ES module | Use an explicit emitted extension |
| `.mts` | ES module | Use an explicit emitted extension |
| `.cts` | CommonJS | CommonJS-style extensionless imports can work |
A .ts file in a package without "type": "module" is normally treated as CommonJS. The .mts and .cts extensions make the choice explicit:
.mtsemits.mjsand is always treated as an ES module..ctsemits.cjsand is always treated as CommonJS..tsfollows the nearest package boundary and its"type"field.
For example, this configuration selects Node.js-compatible behavior:
{
"compilerOptions": {
"module": "nodenext",
"target": "es2022",
"strict": true,
"outDir": "dist"
}
}The module option affects both the JavaScript format TypeScript emits and, with nodenext, the corresponding module resolution rules.
Mapping source extensions to emitted extensions
TypeScript lets an ES module source file refer to the JavaScript file that will exist after compilation.
| Source file | Emitted file | Import from an ES module |
|---|---|---|
| `greet.ts` | `greet.js` | `./greet.js` |
| `parser.mts` | `parser.mjs` | `./parser.mjs` |
| `legacy.cts` | `legacy.cjs` | Usually imported through CommonJS interop |
Do not write ./greet.ts for normal compiled output. Node.js will try to load a TypeScript file, but the compiler normally emits greet.js.
The same rule applies to nested paths. If src/features/main.ts imports src/shared/config.ts, write:
import { config } from "../shared/config.js";Avoid directory imports such as import { config } from "../shared". Node.js ESM resolution does not automatically search a directory's index.js in the same way older CommonJS patterns often did.
Bundler mode is different
A bundler is a build tool such as esbuild, Rollup, or webpack. It reads your imports and combines or transforms files before the application runs. Because the bundler can resolve source files directly, bundler mode accepts extensionless relative imports:
{
"compilerOptions": {
"module": "preserve",
"moduleResolution": "bundler",
"target": "es2022",
"strict": true,
"noEmit": true
}
}With this mode, the following is commonly valid:
import { greet } from "./greet";This import may fail if you later run the emitted files directly with Node.js. Choose the rule based on the actual runtime, not only on whether TypeScript reports an error.
Common mistakes
- Using
.tsin an import that will run from the compileddistdirectory. - Omitting
.jsfrom an ES module import undernodenext. - Assuming
moduleResolution: nodenextcan be combined with an arbitrary module output setting. - Testing only with a bundler, then running the same files directly in Node.js.
- Importing a directory and expecting Node.js ESM to find its index file automatically.
Exercise
A package has "type": "module". The files are src/main.ts and src/math.ts, and TypeScript emits JavaScript into dist. What import should main.ts use to import add from math.ts?
Show answer
import { add } from "./math.js"; because the ES module will run against the emitted dist/math.js file.