next.config.js Options
Next.js 可以通过项目根目录(例如和 package.json
同级)下的 next.config.js
文件进行配置,该文件需要包含一个默认导出。
ECMAScript 模块
next.config.js
是一个常规的 Node.js 模块,而不是 JSON 文件。它会被 Next.js 服务器和构建阶段使用,且不会包含在浏览器构建中。
如果你需要使用 ECMAScript 模块,可以使用 next.config.mjs
:
值得注意的是:目前不支持使用
.cjs
、.cts
或.mts
扩展名的next.config
文件。
配置为函数
你也可以使用函数:
异步配置
从 Next.js 12.1.0 开始,你可以使用异步函数:
阶段
phase
是配置加载时的当前上下文。你可以查看可用的阶段。这些阶段可以从 next/constants
导入:
TypeScript
此功能在 Next.js canary 版本中可用。
如果你在项目中使用 TypeScript,你可以使用 next.config.ts
在配置中使用 TypeScript:
注释部分是你可以放置 next.config.js
允许的配置的地方,这些配置在这个文件中定义。
但是,这些配置都不是必需的,你也不需要理解每个配置的作用。相反,在本节中搜索你需要启用或修改的功能,它们会告诉你该怎么做。
避免使用目标 Node.js 版本中不可用的新 JavaScript 功能。
next.config.js
不会被 Webpack 或 Babel 解析。
本页面记录了所有可用的配置选项:
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.
bundlePagesRouterDependencies
Enable automatic dependency bundling for Pages Router
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` and `next/head`.
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.
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
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.
productionBrowserSourceMaps
Enables browser source map generation during the production build.
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.
Runtime Config
Add client and server runtime configuration to your Next.js app.
serverExternalPackages
Opt-out specific dependencies from the dependency bundling enabled by `bundlePagesRouterDependencies`.
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
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.
webVitalsAttribution
Learn how to use the webVitalsAttribution option to pinpoint the source of Web Vitals issues.
Custom Webpack Config
Learn how to customize the webpack config used by Next.js