I try to setup a NodeJS child process with arguments. If I run the child process with node it works fine but if I run in with casperjs instead, it doesn't work. I made sure that casperjs is running properly, with another casperjs script which works fine. Here is my setup:
parent.js
var exec = require('child_process').exec;
exec('node child.js', {
env: {
number: 123
}
}, function(err, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (err !== null) {
console.log('exec error: ' + err);
}
});
parent2.js
var exec = require('child_process').exec;
exec('casperjs child.js', {
env: {
number: 123
}
}, function(err, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (err !== null) {
console.log('exec error: ' + err);
}
});
child.js
var number = process.env.number;
console.log(typeof(number));
number = parseInt(number, 10);
console.log((number));
Output
$ node parent.js
stdout: string
123
stderr:
$ node parent2.js
stdout: Fatal: [Errno 2] No such file or directory; did you install phantomjs?
stderr:
exec error: Error: Command failed:
Why can I not use arguments when running the child process with casperjs?
Child process runs in a separate process
You have to pass environment variables while you are calling "exec" https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
Example path for windows
Double slash \\ used to escape inside string
Example path for linux
/opt/node only required if you have installed in custom location. As default node will go in default visible PATH
Otherwise add environment variables to /etc/profile
Refer How to set environment variable for everyone under my linux system?