You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>
> **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.
100
100
101
101
When using the `next.config.js` file, you can add some type checking in your IDE using JSDoc as below:
102
102
@@ -111,6 +111,42 @@ const nextConfig = {
111
111
module.exports= nextConfig
112
112
```
113
113
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
+
importtype { NextConfig } from'next'
132
+
133
+
// Top-level await and dynamic import are supported
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
+
114
150
### Statically Typed Links
115
151
116
152
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.
0 commit comments