next.config.js のオプション
Next.js は、プロジェクトディレクトリのルート(例えば、package.jsonと同じ場所)にある 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
を使用できます。
// @ts-check
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* config options here */
}
export default nextConfig
知っておくと良いこと:
.cjs
、.cts
、または.mts
拡張子を持つnext.config
は現在サポートされていません。
関数としての構成
関数を使用することもできます。
// @ts-check
export default (phase, { defaultConfig }) => {
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* config options here */
}
return nextConfig
}
非同期構成
Next.js 12.1.0以降では、非同期関数を使用できます。
// @ts-check
module.exports = async (phase, { defaultConfig }) => {
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* config options here */
}
return nextConfig
}
フェーズ
phase
は、構成が読み込まれる現在のコンテキストです。利用可能なフェーズを確認できます。フェーズは next/constants
からインポートできます。
// @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 を使用できます。
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 によって解析されません。
このページでは、利用可能なすべての設定オプションについて説明します。
assetPrefix
basePath
bundlePagesRouterDependencies
compress
crossOrigin
devIndicators
distDir
env
eslint
exportPathMap
generateBuildId
generateEtags
headers
httpAgentOptions
images
onDemandEntries
optimizePackageImports
output
pageExtensions
poweredByHeader
productionBrowserSourceMaps
reactStrictMode
redirects
rewrites
Runtime Config
serverExternalPackages
trailingSlash
transpilePackages
turbo
typescript
urlImports
useLightningcss
webpack
webVitalsAttribution
これは役立ちましたか?