Reputation: 100290
I am wondering what the correct way is to add "execArgs" to a Node process -
we have:
const cp = require('child_process');
const n = cp.spawn('node', ['some-file.js'], {});
but what if I want to add an execArg like so:
const n = cp.spawn('node --harmony', ['some-file.js'], {});
I don't think that is the right way to do it, and the docs don't seem to demonstrate this?
Is this the correct way?
const n = cp.spawn('node', ['--harmony','some-file.js'], {});
Upvotes: 0
Views: 127
Reputation: 19428
According to the docs for child_process.spawn()
it clearly states that args is an array of string arguments that is passed in as the second argument.
The child_process.spawn() method spawns a new process using the given command, with command line arguments in args. If omitted, args defaults to an empty array.
A third argument may be used to specify additional options, with these defaults:
{ cwd: undefined, env: process.env }
const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
Based on the above pulled from the child_process docs, the below would be correct.
const n = cp.spawn('node', ['--harmony','some-file.js']);
Upvotes: 1