Skip to content

Commit ab2e234

Browse files
committed
refactor: clean environment variables in dev and start commands to exclude pnpm injected npm_config_* settings
1 parent 60fb981 commit ab2e234

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

src/commands/dev.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,21 @@ export const devCommand = new Command('dev')
4646
// 检测 development 源码是否可用(仅 monorepo 环境)
4747
const devSourcesAvailable = fs.existsSync(path.join(cwd, 'node_modules/zhin.js/src'));
4848

49-
// 设置环境变量
50-
const nodeOptions = (process.env.NODE_OPTIONS || '')
49+
// 构建干净的环境变量:剔除 pnpm 注入的 npm_config_* 以免子进程中
50+
// npm 读到不认识的配置项而报 warn 甚至阻塞
51+
const cleanEnv: Record<string, string | undefined> = {};
52+
for (const [key, value] of Object.entries(process.env)) {
53+
if (/^npm_/i.test(key)) continue;
54+
cleanEnv[key] = value;
55+
}
56+
const nodeOptions = (cleanEnv.NODE_OPTIONS || '')
5157
+ (devSourcesAvailable ? ' --conditions=development' : '');
5258
const env = {
53-
...process.env,
59+
...cleanEnv,
5460
NODE_ENV: 'development',
5561
ZHIN_DEV_MODE: 'true',
5662
ZHIN_HMR_PORT: options.port,
5763
ZHIN_VERBOSE: options.verbose ? 'true' : 'false',
58-
// 仅当框架源码可用时才添加 development 条件(monorepo 环境)
5964
NODE_OPTIONS: nodeOptions
6065
};
6166

src/commands/start.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ export const startCommand = new Command('start')
3636

3737
// 启动机器人的函数
3838
const startBot = async (): Promise<ChildProcess> => {
39-
// 设置环境变量
39+
// 构建干净的环境变量:剔除 pnpm 注入的 npm_config_* 以免子进程中
40+
// npm 读到不认识的配置项而报 warn 甚至阻塞
41+
const cleanEnv: Record<string, string | undefined> = {};
42+
for (const [key, value] of Object.entries(process.env)) {
43+
if (/^npm_/i.test(key)) continue;
44+
cleanEnv[key] = value;
45+
}
4046
const env = {
41-
...process.env,
47+
...cleanEnv,
4248
NODE_ENV: 'production'
4349
};
4450
// 配置stdio

0 commit comments

Comments
 (0)