Skip to content

Commit 09c5a7c

Browse files
devjiwonchoiicyJosephabdonrd
authored
docs: add next.config.ts Node.js native resolver (#83561)
This PR adds docs for using Node.js native TS resolver for `next.config.ts`. It states: - Supports Node.js native TS resolver - Why use it - How to use it - Restrictions - Why use `next.config.mts` I didn't mention how much it's faster compared to the legacy resolver (~68% faster in the hello world app - [link](#83240)) because the experience may vary for users. --------- Co-authored-by: Joseph <joseph.chamochumbi@vercel.com> Co-authored-by: Abdón Rodríguez <a@abdonrd.com>
1 parent 8248d6a commit 09c5a7c

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

docs/01-app/03-api-reference/05-config/02-typescript.mdx

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Next.js generates global helpers for App Router route types. These are available
8282

8383
## Examples
8484

85-
### Type checking `next.config.ts`
85+
### Type Checking Next.js Configuration Files
8686

8787
You can use TypeScript and import types in your Next.js configuration by using `next.config.ts`.
8888

@@ -96,7 +96,7 @@ const nextConfig: NextConfig = {
9696
export default nextConfig
9797
```
9898

99-
> **Good to know**: Module resolution in `next.config.ts` is currently limited to `CommonJS`. This may cause incompatibilities with ESM only packages being loaded in `next.config.ts`.
99+
Module resolution in `next.config.ts` is currently limited to CommonJS. However, ECMAScript Modules (ESM) syntax is available when [using Node.js native TypeScript resolver](#using-nodejs-native-typescript-resolver-for-nextconfigts) for Node.js v22.10.0 and higher.
100100

101101
When using the `next.config.js` file, you can add some type checking in your IDE using JSDoc as below:
102102

@@ -111,6 +111,42 @@ const nextConfig = {
111111
module.exports = nextConfig
112112
```
113113

114+
### Using Node.js Native TypeScript Resolver for `next.config.ts`
115+
116+
> **Note**: Available on Node.js v22.10.0+ and only when the feature is enabled. Next.js does not enable it.
117+
118+
Next.js detects the [Node.js native TypeScript resolver](https://nodejs.org/api/typescript.html) via [`process.features.typescript`](https://nodejs.org/api/process.html#processfeaturestypescript), added in **v22.10.0**. When present, `next.config.ts` can use native ESM, including top‑level `await` and dynamic `import()`. This mechanism inherits the capabilities and limitations of Node's resolver.
119+
120+
In Node.js versions **v22.18.0+**, `process.features.typescript` is enabled by default. For versions between **v22.10.0** and **22.17.x**, opt in with `NODE_OPTIONS=--experimental-transform-types`:
121+
122+
```bash filename="Terminal"
123+
NODE_OPTIONS=--experimental-transform-types next <command>
124+
```
125+
126+
#### For CommonJS Projects (Default)
127+
128+
Although `next.config.ts` supports native ESM syntax on CommonJS projects, Node.js will still assume `next.config.ts` is a CommonJS file by default, resulting in Node.js reparsing the file as ESM when module syntax is detected. Therefore, we recommend using the `next.config.mts` file for CommonJS projects to explicitly indicate it's an ESM module:
129+
130+
```ts filename="next.config.mts"
131+
import type { NextConfig } from 'next'
132+
133+
// Top-level await and dynamic import are supported
134+
const flags = await import('./flags.js').then((m) => m.default ?? m)
135+
136+
const nextConfig: NextConfig = {
137+
/* config options here */
138+
typedRoutes: Boolean(flags?.typedRoutes),
139+
}
140+
141+
export default nextConfig
142+
```
143+
144+
#### For ESM Projects
145+
146+
When `"type"` is set to `"module"` in `package.json`, your project uses ESM. Learn more about this setting [in the Node.js docs](https://nodejs.org/api/packages.html#type). In this case, you can write `next.config.ts` directly with ESM syntax.
147+
148+
> **Good to know**: When using `"type": "module"` in your `package.json`, all `.js` and `.ts` files in your project are treated as ESM modules by default. You may need to rename files with CommonJS syntax to `.cjs` or `.cts` extensions if needed.
149+
114150
### Statically Typed Links
115151

116152
Next.js can statically type links to prevent typos and other errors when using `next/link`, improving type safety when navigating between pages.
@@ -479,7 +515,7 @@ When you need to declare custom types, you might be tempted to modify `next-env.
479515

480516
| Version | Changes |
481517
| --------- | ------------------------------------------------------------------------------------------------------------------------------------ |
482-
| `v15.0.0` | [`next.config.ts`](#type-checking-nextconfigts) support added for TypeScript projects. |
518+
| `v15.0.0` | [`next.config.ts`](#type-checking-nextjs-configuration-files) support added for TypeScript projects. |
483519
| `v13.2.0` | Statically typed links are available in beta. |
484520
| `v12.0.0` | [SWC](/docs/architecture/nextjs-compiler) is now used by default to compile TypeScript and TSX for faster builds. |
485521
| `v10.2.1` | [Incremental type checking](https://www.typescriptlang.org/tsconfig#incremental) support added when enabled in your `tsconfig.json`. |

0 commit comments

Comments
 (0)