Skip to main content
Next.js is configured through a next.config.js (or next.config.ts) file at the root of your project.

next.config.js basics

next.config.js
Use JSDoc to get TypeScript type checking in a .js file, or use next.config.ts for full TypeScript support:
next.config.ts

Environment variables

Built-in .env support

Next.js loads environment variables from .env files automatically:
.env.local
Access them in server-side code:
pages/api/data.ts

Exposing variables to the browser

Variables prefixed with NEXT_PUBLIC_ are included in the client-side bundle:
.env.local
pages/index.tsx
Never prefix secret values (API keys, database passwords) with NEXT_PUBLIC_. They will be visible in the browser.

Runtime environment variables

For values that differ between deployments (not build time), access them in getServerSideProps or API routes at runtime:

TypeScript

Next.js has zero-configuration TypeScript support. Create or leave a tsconfig.json at the root and run next dev—Next.js configures it automatically.

Strict mode

Enable strict type checking:
tsconfig.json

Pages Router TypeScript types

Key types from the next package:

ESLint

Next.js includes a built-in ESLint configuration. Run it with:
create-next-app sets this up automatically. For manual setup:
.eslintrc.json
The next/core-web-vitals preset is stricter than next and enables rules that affect Core Web Vitals.

Absolute imports

Configure the baseUrl in tsconfig.json (or jsconfig.json for JavaScript projects) to use absolute imports:
tsconfig.json
Now you can import from the project root instead of using relative paths:

Module aliases

For custom path aliases:
tsconfig.json

Headers, redirects, and rewrites

Configure HTTP headers, redirects, and URL rewrites in next.config.js:
next.config.js

Customizing the build directory

The default build output directory is .next. You can change it:
next.config.js

Page extensions

By default, Next.js looks for .js, .jsx, .ts, and .tsx files in pages/. You can customize this:
next.config.js
With this config, pages/index.page.tsx maps to / and plain .tsx files in pages/ are ignored (useful for co-locating tests with pages).