NextJS
默认加载的是.env.local
,所以可以通过一个巧妙地办法,直接复制对应的文件到.env.local
中,然后启动。
根目录添加不同环境的.env
➜ chnqoo-recollections-service git:(master) ✗ find . -type f -name ".env*" ./.env.production ./.env.local ./.env.alpha ./.env.staging ./.env.development
添加脚本到package.json
"test:staging" : "cp .env.staging .env.local && next" ,"test:development" : "cp .env.development .env.local && next" ,"test:alpha" : "cp .env.alpha .env.local && next" ,"test:production" : "cp .env.production .env.local && next" ,
运行脚本 这里以启动Alpha
环境为例:
➜ chnqoo-recollections-service git:(master) ✗ npm run test :alpha > typescript-nextjs-starter@1.0.0 test :alpha > cp .env.alpha .env.local && next ▲ Next.js 14.1.0 - Local: http://localhost:3000 - Environments: .env.local, .env.development warning ../package.json: No license field ✓ Ready in 7s ✓ Compiled /api/common/testHelloWorld in 408ms (20 modules) ✓ Compiled /favicon.ico in 467ms (46 modules)