Skip to content

Commit 0286ac5

Browse files
devjiwonchoiabdonrd
andcommitted
add esm
Co-authored-by: Abdón Rodríguez <[email protected]>
1 parent 71a9dd9 commit 0286ac5

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ In Node.js versions **v22.18.0+**, `process.features.typescript` is enabled by d
123123
NODE_OPTIONS=--experimental-transform-types next <command>
124124
```
125125

126-
By default, Node treats `.ts` files as CommonJS. If `next.config.ts` uses ESM syntax, Node re-parses it as ESM, which works but prints a warning about performance overhead. To avoid this extra pass, use `next.config.mts` to declare ESM up front.
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:
127129

128130
```ts filename="next.config.mts"
129131
import type { NextConfig } from 'next'
@@ -139,6 +141,19 @@ const nextConfig: NextConfig = {
139141
export default nextConfig
140142
```
141143

144+
#### For ESM Projects
145+
146+
If your project is configured as an ESM project by adding `"type": "module"` to your `package.json` (see [Node.js documentation](https://nodejs.org/api/packages.html#type)), you can use `next.config.ts` directly with ESM syntax:
147+
148+
```json filename="package.json"
149+
{
150+
"type": "module"
151+
// ... other package.json fields
152+
}
153+
```
154+
155+
> **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.
156+
142157
### Statically Typed Links
143158

144159
Next.js can statically type links to prevent typos and other errors when using `next/link`, improving type safety when navigating between pages.

0 commit comments

Comments
 (0)