> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# TypeScript Configuration

Modern.js ships shared TypeScript presets in `@modern-js/tsconfig`. A project usually needs just two files: the main `tsconfig.json`, and a `tsconfig.server.json` only when it has server-side code.

## Presets

| Preset                       | Purpose                                                                                                                                                                                                                                                                   |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `@modern-js/tsconfig/base`   | Main config. `module: ESNext`, `moduleResolution: bundler`, `jsx: react-jsx`, `target: ES2022`, `strict`. Used by the editor, Rspack alias resolution and type-checking.                                                                                                  |
| `@modern-js/tsconfig/server` | Reference preset with the four server-side emit overrides: `module: NodeNext`, `moduleResolution: NodeNext`, `noEmit: false`, `declaration: false`. For tools that support `extends` arrays (tsc >= 5.0); the recommended `tsconfig.server.json` inlines the same fields. |
| `@modern-js/tsconfig/legacy` | The previous `base` (`module: commonjs`, `moduleResolution: node`, `target: ES2015`). Rollback aid only; TypeScript 6 reports these options as deprecated.                                                                                                                |
| `@modern-js/tsconfig/react`  | Deprecated alias of `base`. Use `base` directly.                                                                                                                                                                                                                          |

## Two-file layout

`tsconfig.json` is the single main config, and every path alias (`paths`) lives here:

```json title="tsconfig.json"
{
  "extends": "@modern-js/tsconfig/base",
  "compilerOptions": {
    "noEmit": true,
    "isolatedModules": true,
    "moduleDetection": "force",
    "noEmitOnError": true,
    "types": ["node"],
    "paths": {
      "@/*": ["./src/*"],
      "@shared/*": ["./shared/*"]
    }
  },
  "include": ["src", "shared", "config", "modern.config.ts"]
}
```

`isolatedModules`, `moduleDetection: force` and `noEmitOnError` are set in the project file rather than in `base`, so upgrading `@modern-js/tsconfig` does not add these checks to an existing project. With `noEmitOnError: true`, a type error in `api/` fails `modern build`; without it, BFF type errors are only reported. New projects get them from the template; add them to an existing `tsconfig.json` when you are ready to fix what they report.

When the project has a BFF (`api/`) or a custom server (`server/`), add `"@api/*": ["./api/lambda/*"]` to `paths`, add `api` and `server` to `include`, and create:

```json title="tsconfig.server.json"
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "noEmit": false,
    "declaration": false
  },
  "include": ["api", "server", "shared"]
}
```

The framework uses this file to compile `api/`, `server/` and `shared/` (with the project's own `tsc` command) and to run server source at dev time. The four `compilerOptions` override only their own keys; everything else, including `paths`, is inherited from `tsconfig.json`. They are the same fields as `@modern-js/tsconfig/server`, inlined so tools still on ts-node 10 (which does not support `extends` arrays) can read the file.

## Why two files

Bundlers expect `module: ESNext`, while the type check of server code should resolve modules the way Node does (`NodeNext`). No single `module` value satisfies both, so the server side overrides the module options separately. The output format is decided by the framework from `package.json#type`, independently of these two fields.

## Without `tsconfig.server.json`

The server config is resolved as `server.tsconfigPath` → `tsconfig.server.json` → `tsconfig.json`. When `tsconfig.server.json` is picked up by convention, a one-line message shows which file is used. When it falls back to `tsconfig.json`, the output still follows `package.json#type` (a CommonJS project still gets `require`) and nothing is reported; only when its `module` is an ES module value and the project is not `type: module` is the server code compiled with `module: CommonJS`, as with the previous base preset. `type: module` projects are never rewritten. See [server.tsconfigPath](/configure/app/server/tsconfig-path.md) for the full rule.

## Migrating an existing project

The previous create template wrote `"baseUrl": "./"` into `tsconfig.json`. TypeScript 6 reports TS5101 for `baseUrl` (deprecated) and TypeScript 7 reports TS5102 (removed). Projects that upgrade Modern.js but stay on TypeScript 5.x are not affected, and the framework build keeps honoring `baseUrl`.

Upgrading dependencies never rewrites project files. To migrate, run the `modernjs-migrate-tsconfig` Skill:

```bash
# Install the Skill (--agent: claude-code / codex / cursor / ...)
npx skills add web-infra-dev/modern.js --skill modernjs-migrate-tsconfig

# Or run the script directly; <skillDir> is where the Skill was installed
node <skillDir>/scripts/migrate-tsconfig.mjs <projectDir>
```

Results go to `changed` (applied) and `manual` (left to you) in `<projectDir>/.agents/runs/modernjs-migrate-tsconfig/report.json`. The script is safe to run again.

| Project state                                                                                                      | What the script does                                                                                                                                                                                 |
| ------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `baseUrl` is set                                                                                                   | Prefixes every `paths` entry with the old `baseUrl` so it is relative to `tsconfig.json`, adds a `"*"` mapping when source files use bare imports resolved through `baseUrl`, then removes `baseUrl` |
| `api/lambda` exists and `paths` has no `@api/*`                                                                    | Adds `"@api/*": ["./api/lambda/*"]`                                                                                                                                                                  |
| Not `type: module`, has `api/` or `server/`, no `tsconfig.server.json` and no `server.tsconfigPath`                | Writes the `tsconfig.server.json` shown above                                                                                                                                                        |
| It writes `tsconfig.server.json` and `api/`, `server/` or `shared/` has a relative `import()` without an extension | Lists each one as a manual item: under NodeNext `import()` stays a native ES module import, so it needs a `.js` extension (otherwise tsc reports TS2835 and the module is not found at runtime)      |
| `tsconfig.json` has comments or trailing commas                                                                    | Leaves the file untouched and lists the changes above as manual items                                                                                                                                |
| `baseUrl` is declared in a config it `extends`                                                                     | Leaves that config untouched and lists it as a manual item                                                                                                                                           |

The rewrite is equivalent to `baseUrl`. For example, with `baseUrl: "./src"` and a source file importing `utils/log` (resolved to `src/utils/log.ts`):

```json title="Before"
{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "@/*": ["*"],
      "@shared/*": ["../shared/*"]
    }
  }
}
```

```json title="After"
{
  "compilerOptions": {
    "paths": {
      "@/*": ["./src/*"],
      "@shared/*": ["./shared/*"],
      "*": ["./src/*"]
    }
  }
}
```

Without such bare imports no `"*"` mapping is added. Without the Skill, apply the table above by hand.

## Notes

- Never declare `paths` in `tsconfig.server.json`: TypeScript replaces `paths` wholesale instead of merging it key by key.
- `baseUrl` is no longer needed. Relative entries in `paths` are resolved against the directory of the file that declares them.
- If a project keeps `baseUrl` on TypeScript 6, the server type check ignores TS5101 and the other deprecations and does not fail because of them; other type errors still fail the build.
- The server compile does not rely on the TypeScript JS API, so BFF and custom servers keep working on TypeScript 7; ts-node is no longer needed.
- Keep `@modern-js/tsconfig` and `@modern-js/app-tools` on the same version: the framework has to know how to compile `api/`, `server/` and `shared/` from a bundler-mode `tsconfig.json`.
