If you want to pass some arguments you can go with:
const { execFile } = require('child_process');
const child = execFile('node', ['--version'], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
If you want to pass custom stream as standard input to the child process use spawn:
const { spawn } = require('child_process');
// Child will use parent's stdios
spawn('prg', [], { stdio: 'inherit' });
// Spawn child sharing only stderr
spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });
// Open an extra fd=4, to interact with programs presenting a
// startd-style interface.
spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] });
All examples from the docs: https://nodejs.org/api/child_process.html#child_process_options_stdio
exec()can contain space-separated arguments following the executable name.exec(). The user enters the arguments after the executable file has been called.