next.config.js
Next.js 可以通过项目目录根目录(例如,在 package.json
旁边)的 next.config.js
文件进行配置,使用默认导出。
next.config.js
// @ts-check
/** @type {import('next').NextConfig} */
const nextConfig = {
/* 在此处配置选项 */
}
module.exports = nextConfig
ECMAScript 模块
next.config.js
是一个常规的 Node.js 模块,不是 JSON 文件。它被 Next.js 服务器和构建阶段使用,不包含在浏览器构建中。
如果你需要 ECMAScript 模块,可以使用 next.config.mjs
:
next.config.mjs
// @ts-check
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* 在此处配置选项 */
}
export default nextConfig
提示:目前不支持带有
.cjs
、.cts
或.mts
扩展名的next.config
。
函数配置
你也可以使用函数:
next.config.mjs
// @ts-check
export default (phase, { defaultConfig }) => {
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* 在此处配置选项 */
}
return nextConfig
}