[英]Child Process Node.js with CasperJs: Cannot find module 'casper'; require statement
[英]Child Process - Node.js with CasperJs: How to include arguments?
我嘗試使用參數設置NodeJS子進程。 如果我運行帶有節點的子進程,則工作正常,但如果運行casperjs,則無法正常工作。 我確保casperjs正常運行,並且另一個casperjs腳本運行正常。 這是我的設置:
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));
輸出量
$ 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:
為什么在使用casperjs運行子進程時不能使用參數?
子進程在單獨的進程中運行
調用“ exec”時必須傳遞環境變量https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
var exec = require('child_process').exec;
exec('casperjs child.js', {
env: {
'PATH': '<your path locations with path delimiter>'
}
},
Windows的示例路徑
c:\\phantomjs\\bin;c:\\casperjs\\bin;C:\\Users\\<username>\\AppData\\Roaming\\npm\\;C:\\Program Files\\nodejs
雙斜杠\\\\用於轉義字符串
Linux的示例路徑
/opt/node:/opt/phantomjs/bin:/opt/casperjs/bin
/ opt / node僅在安裝在自定義位置時才需要。 默認節點將進入默認可見路徑
否則將環境變量添加到/ etc / profile
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.