You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Next.js的默认配置文件为my.next.config.js?

修改Next.js配置文件为自定义文件名的方法

Next.js默认读取next.config.js作为配置文件,要改成my.next.config.js这类自定义文件名,有三种常用方式:

1. 启动命令后直接指定参数

在执行构建或开发命令时,通过--config参数指定自定义配置文件路径:

  • 开发环境启动:next dev --config ./my.next.config.js
  • 生产环境构建:next build --config ./my.next.config.js
  • 生产环境启动:next start --config ./my.next.config.js

2. 修改package.json的脚本命令

把项目根目录下package.json里的脚本命令改成带--config参数的形式,这样每次运行npm脚本时就自动使用自定义配置:

{
  "scripts": {
    "dev": "next dev --config ./my.next.config.js",
    "build": "next build --config ./my.next.config.js",
    "start": "next start --config ./my.next.config.js"
  }
}

3. 使用环境变量指定

通过设置NEXT_CONFIG_FILE环境变量来指向你的自定义配置文件:

  • Linux/macOS终端:export NEXT_CONFIG_FILE=./my.next.config.js
  • Windows CMD:set NEXT_CONFIG_FILE=./my.next.config.js
  • Windows PowerShell:$env:NEXT_CONFIG_FILE="./my.next.config.js"

设置完环境变量后,再执行next dev、next build等命令,Next.js就会自动读取指定的配置文件。

注意:自定义配置文件的导出方式要和默认的next.config.js保持一致,比如使用module.exports导出配置对象(若用ES模块导出,需确保项目支持ES模块配置)。

内容的提问来源于stack exchange,提问作者TheRusskiy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 15:15:32