next.config.js is where you customize framework-level behavior — from allowed image domains to redirects, rewrites, and build settings. This lesson covers the options you'll reach for most often.
What next.config.js Controls
By default, Next.js works with zero configuration, but next.config.js (or next.config.mjs/.ts) lets you customize build behavior, add redirects and rewrites, configure allowed remote image domains, set custom headers, and enable experimental features ahead of their stable release.
The file exports a single configuration object (or a function returning one), which Next.js reads at build time and merges with its internal defaults. You do not need to duplicate settings you're happy leaving at their default value.
The JSDoc type comment gives you autocomplete for every available config option in editors that support TypeScript-aware JavaScript.
Basic Shape of the Config File
/** @type {import('next').NextConfig} */
const nextConfig = {
// options go here
};
module.exports = nextConfig;
The config file must export the configuration object as the module's default export.
You can name the variable anything; only the exported value matters.
Use next.config.mjs for ESM syntax, or next.config.ts for a typed config with recent Next.js versions.
Changes to next.config.js require restarting the dev server to take effect.
next.config.js Options Cheat Sheet
The configuration keys used most often in real projects.
Option
Purpose
images.remotePatterns
Allow-list external domains for next/image
redirects()
Server-side redirects, defined as a function
rewrites()
Proxy incoming paths to different destinations
headers()
Add custom HTTP response headers
reactStrictMode
Enables additional React checks in development
env
Expose build-time environment variables
output: "standalone"
Produce a minimal, self-contained build for Docker
Redirects and Rewrites
Redirects and rewrites are both defined as async functions returning an array of rules. A redirect sends the browser a new URL (changing the address bar); a rewrite serves different content for a URL while keeping the original address bar URL unchanged.
permanent: true sends a 308 status code, telling browsers and search engines the redirect is permanent.
Allowing External Image Domains
For security, next/image refuses to optimize images from domains you haven't explicitly allowed. remotePatterns (the modern replacement for the older domains array) lets you allow-list exact hostnames, protocols, and even path patterns.
Without a matching pattern, next/image throws an error for external image URLs.
remotePatterns supports wildcards in the hostname for subdomains.
Local images imported directly (e.g. import logo from "./logo.png") never need this configuration.
Common Mistakes
Forgetting to restart the dev server after editing next.config.js.
Using the deprecated images.domains array instead of the more flexible remotePatterns.
Confusing redirects (change the URL) with rewrites (keep the URL, change the content).
Putting secrets directly in the env config key, which can end up bundled into client code if misused.
images.remotePatterns allow-lists external domains for next/image optimization.
redirects() and rewrites() are async functions returning arrays of routing rules.
Redirects change the browser's URL; rewrites keep the URL but serve different content.
Config file changes require a dev server restart to take effect.
Pro Tip
Keep next.config.js under version control and treat changes to it with the same review rigor as application code — a misconfigured redirect or header can silently break production traffic in ways that are hard to notice locally.
You've covered core configuration. Next, dive into Next.js Routes and how URLs map to your application's structure in practice.