node.js - 将参数传递给nodejs中的子进程,该子进程在execFile方法中有一些依赖性

标签 node.js dependencies parent-child

我试图将一些参数传递给 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/

相关文章:

java - maven父子多模块项目

c# - ASP.NET MVC3 : Displaying Validation Errors from Child View in Parent View

javascript - 抛出新的 TypeError ('callback provided to sync glob' )?

spring - org.springframework.security :org. springframework.security.web :jar:3. 0.5.RELEASE 的 POM 丢失,没有可用的依赖信息

node.js - 为什么在NextJS中使用res.writeHead会导致永久重定向?

spring - Grails是否默认使用Spring Dependency Injection

android - 搜索在 Android Studio 的 "Choose Library Dependency"中不起作用

c++ - gdb:中断正在运行的进程而不杀死子进程

node.js - 谷歌云功能|无法加载提供的模块 - 错误 [ERR_REQUIRE_ESM] : Must use import to load ES Module:/workspace/index. js

javascript - Angular 2 快速入门 ENOSPC 错误