Vikas
Vikas

Reputation: 141

Passing arguments to child process in nodejs which has some dependency in execFile method

I trying to pass some of the arguments to the child process in node js. I'am actually running a phantom script inside the child process.I'am using execFile method to run my child script. This is how my index.js looks:

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);
        }
    });

And m trying to print the argument which i have passed.But it is not printing anything. This is how m trying to print it in the child process:

console.log(process.argv[1]);

Upvotes: 1

Views: 1950

Answers (1)

Kumar
Kumar

Reputation: 41

You can parse it like this in ThumbnailCreator.js

var system = require('system'); var args = system.args;

args[0] is file name

args[1] will give you value passed in first argument.

Upvotes: 1

Related Questions