# Module Resolution

> How TypeScript finds your imports.

Subject: TypeScript | Lesson 48 of 54 | 4 min read
Interactive lesson: https://codewithanimation.in/typescript/module-resolution
Previous: [tsconfig Compiler Flags](https://codewithanimation.in/typescript/tsconfig-compiler-flags.md) | Next: [Declaration Files (.d.ts)](https://codewithanimation.in/typescript/declaration-files-dts.md)

## Finding Files

When you write `import { x } from "./utils"`, how does TypeScript know where to look?

It uses a strategy called 'Module Resolution'. The two modern strategies you need to know are `Bundler` and `NodeNext`.

## The Extension Problem

If you are using a modern bundler like Next.js or Vite, it's smart enough to resolve `./utils` to `./utils.ts` or `./utils/index.ts`. This is what the `"Bundler"` strategy simulates.

However, modern Node.js running standard ES Modules is strict. It *requires* the file extension in the import (e.g., `import { x } from "./utils.js"`). The `"NodeNext"` strategy enforces this strictness.

## Syntax

```json
// In tsconfig.json
{
  "compilerOptions": {
    // If using Vite, Webpack, or Next.js
    "moduleResolution": "Bundler",
    
    // If building a raw Node.js library
    "moduleResolution": "NodeNext"
  }
}
```

## Common mistake: The .js Illusion

When using `NodeNext`, you must write `import { x } from "./utils.js"` — even though the file on disk is actually named `utils.ts`!

TypeScript expects you to write the extension of the *compiled* file, because it doesn't modify import paths during compilation. This confuses almost every beginner.

## Tip: package.json exports

Both modern resolution strategies heavily respect the `"exports"` field in `package.json`, which allows library authors to hide internal files from being imported by consumers.

## Try it: Try it

Trace how the TS Compiler attempts to resolve an extensionless import under the `Bundler` strategy versus the strict `NodeNext` strategy.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/module-resolution

## Check yourself

### Question 1

Under the `NodeNext` module resolution strategy, how should you import a local file named `utils.ts`?

- A. import { x } from './utils'
- B. import { x } from './utils.ts'
- C. import { x } from './utils.js'

Correct answer: C

TypeScript doesn't rewrite import paths. Since Node.js needs the .js extension at runtime, TypeScript forces you to write the .js extension in your source code, resolving it back to the .ts file under the hood.

## Remember this

- `moduleResolution: "Bundler"` is for projects using Vite, Next.js, or Webpack (extensionless imports allowed).
- `moduleResolution: "NodeNext"` is for strict Node environments.
- Under `NodeNext`, you must import using `.js` extensions even for `.ts` files.
- Resolution strategies define how the compiler mimics the runtime environment's lookup rules.
