What is the next.config.js file?

next.config.js (or next.config.ts / .mjs) is the project-level configuration file that Next.js reads at startup and build. It controls features such as Cache Components, images, redirects, headers, Turbopack, output mode and experimental flags.

Formats

  • next.config.ts is supported natively with full typing via NextConfig.
  • The export can be an object or a function (phase, { defaultConfig }) => config, so you can vary config between dev and build.
  • It runs in Node.js and is not bundled into the app.

Commonly used options in Next.js 16

  • cacheComponents: true and partialPrefetching: true: the modern caching and prefetching model (default for new apps in 16.4).
  • cacheLife: custom cache profiles.
  • images.remotePatterns, qualities, localPatterns (images.domains is deprecated).
  • redirects(), rewrites(), headers(): async functions returning rules.
  • turbopack: top-level Turbopack options such as rules and resolveAlias (moved out of experimental in 16).
  • reactCompiler: true: enable the React Compiler (stable since 16).
  • typedRoutes: true: type-checked links.
  • output: 'standalone' | 'export', basePath, trailingSlash, serverExternalPackages, transpilePackages.
  • adapterPath: deployment adapters (stable since 16.2).

Removed in 16

eslint options (with next lint), serverRuntimeConfig, publicRuntimeConfig, amp, experimental.ppr, experimental.dynamicIO and experimental.turbopack.

Good to know

Config changes require restarting next dev. Do not put secrets in it; anything under env is inlined into the bundle.

Code example

tsx
1// next.config.ts
2import type { NextConfig } from "next";
3
4const nextConfig: NextConfig = {
5 cacheComponents: true,
6 partialPrefetching: true,
7 reactCompiler: true,
8 typedRoutes: true,
9 images: {
10 remotePatterns: [new URL("https://images.example.com/**")],
11 },
12 async redirects() {
13 return [{ source: "/old-blog/:slug", destination: "/blog/:slug", permanent: true }];
14 },
15 async headers() {
16 return [
17 {
18 source: "/(.*)",
19 headers: [{ key: "X-Content-Type-Options", value: "nosniff" }],
20 },
21 ];
22 },
23 turbopack: {
24 resolveAlias: { "lodash": "lodash-es" },
25 },
26};
27
28export default nextConfig;

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app