next.config.js
Learn how to configure your application with next.config.js.
Next.js can be configured through a next.config.js
file in the root of your project directory (for example, by package.json
) with a default export.
ECMAScript Modules
next.config.js
is a regular Node.js module, not a JSON file. It gets used by the Next.js server and build phases, and it's not included in the browser build.
If you need ECMAScript modules, you can use next.config.mjs
:
Good to know: next.config
with the .cjs
, .cts
, or .mts
extensions are currently not supported.
Configuration as a Function
You can also use a function:
Async Configuration
Since Next.js 12.1.0, you can use an async function:
Phase
phase
is the current context in which the configuration is loaded. You can see the available phases. Phases can be imported from next/constants
:
TypeScript
If you are using TypeScript in your project, you can use next.config.ts
to use TypeScript in your configuration:
The commented lines are the place where you can put the configs allowed by next.config.js
, which are defined in this file.
However, none of the configs are required, and it's not necessary to understand what each config does. Instead, search for the features you need to enable or modify in this section and they will show you what to do.
Avoid using new JavaScript features not available in your target Node.js version. next.config.js
will not be parsed by Webpack or Babel.
This page documents all the available configuration options:
Unit Testing (experimental)
Starting in Next.js 15.1, the next/experimental/testing/server
package contains utilities to help unit test next.config.js
files.
The unstable_getResponseFromNextConfig
function runs the headers
, redirects
, and rewrites
functions from next.config.js
with the provided request information and returns NextResponse
with the results of the routing.
The response from unstable_getResponseFromNextConfig
only considers next.config.js
fields and does not consider middleware or filesystem routes, so the result in production may be different than the unit test.
AppDir
No Description
AssetPrefix
No Description
AuthInterrupts
No Description
BasePath
No Description
CacheLife
No Description
Compress
No Description
CrossOrigin
No Description
CssChunking
No Description
DevIndicators
No Description
DistDir
No Description
DynamicIO
No Description
Env
No Description
Eslint
No Description
ExpireTime
No Description
ExportPathMap
No Description
GenerateBuildId
No Description
GenerateEtags
No Description
Headers
No Description
HttpAgentOptions
No Description
Images
No Description
IncrementalCacheHandlerPath
No Description
InlineCss
No Description
Logging
No Description
MdxRs
No Description
OnDemandEntries
No Description
OptimizePackageImports
No Description
Output
No Description
PageExtensions
No Description
PoweredByHeader
No Description
Ppr
No Description
ProductionBrowserSourceMaps
No Description
ReactCompiler
No Description
ReactMaxHeadersLength
No Description
ReactStrictMode
No Description
Redirects
No Description
Rewrites
No Description
SassOptions
No Description
ServerActions
No Description
ServerComponentsHmrCache
No Description
ServerExternalPackages
No Description
StaleTimes
No Description
StaticGeneration
No Description
TrailingSlash
No Description
TranspilePackages
No Description
Turbo
No Description
TypedRoutes
No Description
Typescript
No Description
UrlImports
No Description
UseLightningcss
No Description
Webpack
No Description
WebVitalsAttribution
No Description