import { readFileSync, existsSync } from 'node:fs'; import { resolve, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import { parse } from 'dotenv'; const root = dirname(fileURLToPath(import.meta.url)); process.chdir(root); if (existsSync(resolve('static'))) { throw new Error('Standalone must run in an isolated directory without a legacy static/ folder'); } const buildIdFile = resolve(".next", 'BUILD_ID'); if (!existsSync(buildIdFile)) { throw new Error( '缺少 ' + ".next" + '/BUILD_ID。请使用打包产物并用 node start.mjs 启动,不要使用 next start。', ); } const environment = "production"; const read = (file) => (existsSync(file) ? parse(readFileSync(file)) : {}); const config = { ...read(resolve('config', environment + '.env')), ...read(resolve('config', environment + '.local.env')), ...process.env, APP_ENV: environment, NODE_ENV: 'production', }; const api = (config.API_PROXY_TARGET || config.NEXT_PUBLIC_API_BASE_URL || '').trim(); if (!api) throw new Error('API_PROXY_TARGET 或 NEXT_PUBLIC_API_BASE_URL 必填'); Object.assign(process.env, config); // standalone 入口(不是 next start) await import('./server.js');