我试图将一些参数传递给 Node js中的子进程。我实际上是在子进程内运行虚拟脚本。我正在使用 execFile 方法来运行我的子脚本。这就是我的 index.js 的样子:
var childProcess = require('child_process');
var path = require('path');
var phantomjs = require('phantomjs');
var binPath = phantomjs.path
console.log('inside index method ');
// Set the path as described here: https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/
// Set the path to the phantomjs binary
//var phantomPath = path.join(__dirname, 'phantomjs_linux-x86_64');
// Arguments for the phantom script
var processArgs = [
path.join(__dirname, 'ThumbnailCreator.js'),
'myargs'
];
// Launch the child process
childProcess.execFile(binPath, processArgs, function(error, stdout, stderr) {
console.log('stdout: ', stdout);
console.log('stderr: ', stderr);
if (error !== null) {
console.log('exec error: ', error);
}
});
我试图打印我已经传递的参数。但它没有打印任何东西。这就是我尝试在子进程中打印它的方式:
console.log(process.argv[1]);
最佳答案
您可以在 ThumbnailCreator.js 中像这样解析它
var 系统 = require('系统'); var args = system.args;
args[0]是文件名
args[1] 将为您提供第一个参数中传递的值。
关于node.js - 将参数传递给nodejs中的子进程,该子进程在execFile方法中有一些依赖性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41801055/