Nodejs 将参数传递给 fork 对象

Nodejs Passing argument to fork object

如何将“--max_old_space_size=1024”发送到子进程fork? 我试图将此参数作为 args , argv 发送 请注意,因为我需要通信渠道,所以我无法使用 exec 或 spawn。 所以基本上我想要 运行 我的子进程

require('child_process').fork('myfile.js');

作为

node --max_old_space_size=1024 myfile.js

您正在寻找 the execArgv property of fork's options:

require('child_process').fork('myfile.js', {
  execArgv: ['--max_old_space_size=1024']
});

然后在 myfile.js 你可以通过 process.execArgv:

检查它是否被考虑过
console.log(process.execArgv);

输出应该和直接调用一样:

node --max_old_space_size=1024 myfile.js

这区分了这两种类型的参数:node <node's args> file.js <file's args>