chore(api): use .env.production on server, .env for local dev only
Load production env without .env override; sync-api-env targets .env.production; PM2 NODE_ENV=production; Prisma uses with-api-env.cjs on deploy. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -5,22 +5,18 @@ import { resolve } from 'path';
|
||||
/**
|
||||
* 环境变量加载策略(后加载的文件覆盖先前的同名键):
|
||||
*
|
||||
* - 本地开发(NODE_ENV !== production):仅 `.env` + `.env.local`
|
||||
* `.env.development` 仅作 deploy/sync-api-env.sh 的源模板,不在本地 dev 自动加载
|
||||
* - 本地开发:仅 `.env` + `.env.local`
|
||||
* `.env.development` 仅作参考模板,不自动加载
|
||||
*
|
||||
* - 线上(NODE_ENV=production):`.env.production` → `.env.production.local` → `.env`
|
||||
* 服务器实际运行时以 sync 脚本写入的 `.env` 为准
|
||||
* - 服务器(NODE_ENV=production):仅 `.env.production` + `.env.production.local`
|
||||
* 不使用 `.env`,避免与本地开发配置混淆
|
||||
*/
|
||||
const apiRoot = resolve(__dirname, '..');
|
||||
const nodeEnv = process.env.NODE_ENV ?? 'development';
|
||||
const isProduction = nodeEnv === 'production';
|
||||
|
||||
const layers = isProduction
|
||||
? [
|
||||
resolve(apiRoot, '.env.production'),
|
||||
resolve(apiRoot, '.env.production.local'),
|
||||
resolve(apiRoot, '.env'),
|
||||
]
|
||||
? [resolve(apiRoot, '.env.production'), resolve(apiRoot, '.env.production.local')]
|
||||
: [resolve(apiRoot, '.env'), resolve(apiRoot, '.env.local')];
|
||||
|
||||
for (const file of layers) {
|
||||
|
||||
Reference in New Issue
Block a user