我在bin文件夹中提取了casperjs和phantomjs。
我想在我的nodejs应用程序中使用casperjs。
这是我目前的剧本:
var exec = require('child_process'),
path = require('path');
// Directory paths where I extracted phantomjs and casperjs.
var phantomjs = path.resolve(__dirname, 'bins', 'phantomjs', 'phantomjs');
var casperjs = path.resolve(__dirname, 'bins', 'casperjs', 'bin', 'casperjs');
// I tried to edit the PATH but it doesn't work.
// process.env.PATH = process.env.PATH + ':' + phantomjs;
// process.env.PATH = process.env.PATH + ':' + casperjs;
// Now launch a casperjs script and get result.
var p = exec.spawn(casperjs, ['test.js']);
p.stdout.on('data', function(data) {
console.log(data.toString());
});
当我运行脚本时,出现错误:
Fatal: [Errno 2] No such file or directory; did you install phantomjs?
有没有办法在exec.spawn()中使用phantomjs和casperjs?
编辑:我发现了我的问题,我向系统路径添加了错误的路径。
我添加了文件的路径,但这是愚蠢的,我必须添加目录的路径!
这是正确的代码:
var exec = require('child_process'),
path = require('path');
// Directory paths where I extracted phantomjs and casperjs.
var phantomjs = path.resolve(__dirname, 'bins', 'phantomjs');
var casperjs = path.resolve(__dirname, 'bins', 'casperjs', 'bin');
// console.log(process.env.PATH);
process.env.PATH = process.env.PATH + ':' + phantomjs;
process.env.PATH = process.env.PATH + ':' + casperjs;
// console.log(process.env.PATH);
// Now launch a casperjs script and get result.
var p = exec.spawn('casperjs', ['test.js']);
p.stdout.on('data', function(data) {
console.log(data.toString());
});
答案 0 :(得分:0)
这里有exec(我没有尝试使用spawn):
var exec = require("child_process").exec;
exec('casperjs test scriptName.js',function(err,stdout,stderr){
console.log('stdout: ' + stdout);
});