コンテンツへスキップ
App RouterAPIリファレンスnext.config.js のオプション

next.config.js のオプション

Next.jsは、プロジェクトディレクトリのルート(たとえば、package.jsonのある場所)にあるnext.config.jsファイルをデフォルトのエクスポート付きで使用することで設定できます。

next.config.js
// @ts-check
 
/** @type {import('next').NextConfig} */
const nextConfig = {
  /* config options here */
}
 
module.exports = nextConfig

ECMAScriptモジュール

next.config.jsは、JSONファイルではなく、通常のNode.jsモジュールです。これはNext.jsサーバーとビルドフェーズで使用され、ブラウザビルドには含まれません。

ECMAScriptモジュールが必要な場合は、next.config.mjsを使用できます

next.config.mjs
// @ts-check
 
/**
 * @type {import('next').NextConfig}
 */
const nextConfig = {
  /* config options here */
}
 
export default nextConfig

知っておくと良いこと: .cjs.cts、または.mts拡張子のnext.configは、現在サポートされていません

関数としての設定

関数を使用することもできます

next.config.mjs
// @ts-check
 
export default (phase, { defaultConfig }) => {
  /**
   * @type {import('next').NextConfig}
   */
  const nextConfig = {
    /* config options here */
  }
  return nextConfig
}

非同期設定

Next.js 12.1.0以降、非同期関数を使用できます

next.config.js
// @ts-check
 
module.exports = async (phase, { defaultConfig }) => {
  /**
   * @type {import('next').NextConfig}
   */
  const nextConfig = {
    /* config options here */
  }
  return nextConfig
}

フェーズ

phaseは、構成がロードされる現在のコンテキストです。利用可能なフェーズを参照できます。フェーズはnext/constantsからインポートできます

next.config.js
// @ts-check
 
const { PHASE_DEVELOPMENT_SERVER } = require('next/constants')
 
module.exports = (phase, { defaultConfig }) => {
  if (phase === PHASE_DEVELOPMENT_SERVER) {
    return {
      /* development only config options here */
    }
  }
 
  return {
    /* config options for all phases except development here */
  }
}

TypeScript

この機能は、Next.js canaryから利用できます。

プロジェクトでTypeScriptを使用している場合は、next.config.tsを使用して、設定でTypeScriptを使用できます

next.config.ts
import type { NextConfig } from 'next'
 
const nextConfig: NextConfig = {
  /* config options here */
}
 
export default nextConfig

コメント行は、next.config.js で許可されている設定を記述できる場所です。これらの設定はこちらのファイルで定義されています

ただし、設定は必須ではなく、それぞれの設定が何をするのかを理解する必要はありません。代わりに、このセクションで有効化または変更したい機能を探すと、何をすればよいかがわかります。

ターゲットの Node.js バージョンで利用できない新しい JavaScript 機能の使用は避けてください。next.config.js は Webpack や Babel によって解析されません。

このページでは、利用可能なすべての設定オプションについて説明します。

appDir

Enable the App Router to use layouts, streaming, and more.

assetPrefix

Learn how to use the assetPrefix config option to configure your CDN.

basePath

Use `basePath` to deploy a Next.js application under a sub-path of a domain.

compress

Next.js provides gzip compression to compress rendered content and static files, it only works with the server target. Learn more about it here.

crossOrigin

Use the `crossOrigin` option to add a crossOrigin tag on the `script` tags generated by `next/script`.

cssChunking

Use the `cssChunking` option to control how CSS files are chunked in your Next.js application.

devIndicators

Optimized pages include an indicator to let you know if it's being statically optimized. You can opt-out of it here.

distDir

Set a custom build directory to use instead of the default .next directory.

env

Learn to add and access environment variables in your Next.js application at build time.

eslint

Next.js reports ESLint errors and warnings during builds by default. Learn how to opt-out of this behavior here.

expireTime

Customize stale-while-revalidate expire time for ISR enabled pages.

exportPathMap

Customize the pages that will be exported as HTML files when using `next export`.

generateBuildId

Configure the build id, which is used to identify the current build in which your application is being served.

generateEtags

Next.js will generate etags for every page by default. Learn more about how to disable etag generation here.

headers

Add custom HTTP headers to your Next.js app.

httpAgentOptions

Next.js will automatically use HTTP Keep-Alive by default. Learn more about how to disable HTTP Keep-Alive here.

images

Custom configuration for the next/image loader

cacheHandler

Configure the Next.js cache used for storing and revalidating data to use any external service like Redis, Memcached, or others.

logging

Configure how data fetches are logged to the console when running Next.js in development mode.

mdxRs

Use the new Rust compiler to compile MDX files in the App Router.

onDemandEntries

Configure how Next.js will dispose and keep in memory pages created in development.

optimizePackageImports

API Reference for optimizePackageImports Next.js Config Option

output

Next.js automatically traces which files are needed by each page to allow for easy deployment of your application. Learn how it works here.

pageExtensions

Extend the default page extensions used by Next.js when resolving pages in the Pages Router.

poweredByHeader

Next.js will add the `x-powered-by` header by default. Learn to opt-out of it here.

ppr

Learn how to enable Partial Prerendering in Next.js.

productionBrowserSourceMaps

Enables browser source map generation during the production build.

reactCompiler

Enable the React Compiler to automatically optimize component rendering.

reactMaxHeadersLength

The maximum length of the headers that are emitted by React and added to the response.

reactStrictMode

The complete Next.js runtime is now Strict Mode-compliant, learn how to opt-in

redirects

Add redirects to your Next.js app.

rewrites

Add rewrites to your Next.js app.

sassOptions

Configure Sass options.

serverActions

Configure Server Actions behavior in your Next.js application.

serverComponentsHmrCache

Configure whether fetch responses in Server Components are cached across HMR refresh requests.

serverExternalPackages

Opt-out specific dependencies from the Server Components bundling and use native Node.js `require`.

staleTimes

Learn how to override the invalidation time of the Client Router Cache.

staticGeneration*

Learn how to configure static generation in your Next.js application.

trailingSlash

Configure Next.js pages to resolve with or without a trailing slash.

transpilePackages

Automatically transpile and bundle dependencies from local packages (like monorepos) or from external dependencies (`node_modules`).

turbo

Configure Next.js with Turbopack-specific options

typedRoutes

Enable experimental support for statically typed links.

typescript

Next.js reports TypeScript errors by default. Learn to opt-out of this behavior here.

urlImports

Configure Next.js to allow importing modules from external URLs.

useLightningcss

Enable experimental support for Lightning CSS.

webpack

Learn how to customize the webpack config used by Next.js

webVitalsAttribution

Learn how to use the webVitalsAttribution option to pinpoint the source of Web Vitals issues.