12

If we fork a child_process in Node, how can we pass node parameters to the child_process?

https://nodejs.org/api/child_process.html

Specifically I would like to spawn ~20 processes, and would like to limit the memory usage of each by using --v8-options, but I can't find any examples of doing this - is this possible or do the child processes assume the same node parameters as the parent?

the parent would be:

node foo.js

and the children would be

node --some-flag=bar baz.js

...

I am looking to pass node options using

child_process.fork()

but if it's only possible with

spawn()

or

exec()

then I guess I will take what I can get.

As a simple example, the following will not run Node.js with the --harmony flag

   var cp = require('child_process');

   var args  = ['--harmony'];

   var n = cp.fork(filePath, args , Object.create(process.env));
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 1
    it looks like fork documentation says it accepts a list of args too https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options – dm03514 Dec 04 '15 at 20:11
  • I think those arguments just go to your program, not to node.js itself, in other words, those arguments are not to V8, just to your program – Alexander Mills Dec 04 '15 at 20:31
  • I tried to provide an example of why that doesn't work in the OP – Alexander Mills Dec 04 '15 at 20:36

1 Answers1

19

You'll need to set the execArgv option to fork.

If you don't, you'll get the same option as the node process you're 'forking' (it actually is just a spawn, not a POSIX-fork).

So you could do something like this:

var n = cp.fork(modname, {execArgv: ['--harmony']});

If you want to pass on the node-options from the parent:

var n = cp.fork(modname, {execArgv: process.execArgv.concat(['--harmony'])}

Warning: child_process has a safeguard against the -e switch that you are circumventing with that! So don't do this from the command line with an -e or -p. You will be creating a new process with a script that is the same as that from the parent – a fork bomb.

If you still want to be able to pass on options to fork via the environment, you could do something like this:

var cp = require('child_process');
var opts = Object.create(process.env);
opts.execArgv = ['--harmony'];

var n = cp.fork(filePath, opts);

Another option may be to alter process.execArgv (like process.execArgv.push('--harmony')) but I am pretty sure that is a bad idea and might result in strange behaviour elswhere.

Patrick J. S.
  • 2,885
  • 19
  • 26
  • thanks for the answer and understanding my question. I will try it out. I see no reason why it should less possible to do it with cp.fork() than with exec or spawn unless you can think of a good reason for that. – Alexander Mills Dec 05 '15 at 19:02
  • @AlexMills: [the code for for `fork` is quite readable](https://github.com/nodejs/node/blob/master/lib/child_process.js#L19) (apart from the argument magic in the first lines), it essentially is a convenience function for spawn. Without all the magic it is something like this: `cp.spawn(process.execPath, (options.execArgs || process.execArgs).concat([moduleName], args), options)` the problem is that args comes after the module name, not before. They have thought of that and you can supply args to node as an option (as stated in the docs). – Patrick J. S. Dec 05 '15 at 19:45
  • trying to confirm that this works, but can't find how to log which v8 flags are being used by a node process.. – Alexander Mills Dec 07 '15 at 03:11
  • nevermind, you can confirm the execArgs are passed to the child_process by looking at process.execArgv in the child proc – Alexander Mills Dec 07 '15 at 03:33