Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments to execFile in node

I am trying to connect to browserstack using their binary and passing my key as an argument. if I do this in a terminal window:

./BrowserStackLocal --key ${BROWSERSTACK_KEY} 

Connection succeeds, my key is passed as expected. However I wish to run this binary via node using execFile. Below is my code

const { execFile } = require('child_process');
function getConnection() {
    execFile('./BrowserStackLocal', ['--key ${BROWSERSTACK_KEY}'], (err, stdout, stderr)  => {
        if (err) {
            console.log(err);
        } else
            console.log(stdout);
    });
}

However when I run my function I get the following:

BrowserStackLocal v7.1

 *** Error: Atleast one argument is required!

To test an internal server, run:
./BrowserStackLocal --key <KEY>
Example:
./BrowserStackLocal --key DsVSdoJPBi2z44sbGFx1

To test HTML files, run:
./BrowserStackLocal --key <KEY> --folder <full path to local folder>
Example:
./BrowserStackLocal --key DsVSdoJPBi2z44sbGFx1 --folder /Applications/MAMP/htdocs/example/

So it does not see my key. I have followed the guide here: https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback and I thought I was passing the argument in the correct manner, but I am obviously doing something wrong. Can someone help me out here? Thanks!

like image 624
user1523236 Avatar asked Apr 30 '18 16:04

user1523236


People also ask

How do I pass command line arguments to a node js program?

In Node. js, all command-line arguments received by the shell are given to the process in an array called argv(arguments-values). Method 1: Using process. argv: It is the simplest way to receive arguments with the help of process.

Which object holds arguments pass through a node command?

The argument vector is an array available from process. argv in your Node. js script. The array contains everything that's passed to the script, including the Node.

What is node Child_process?

The node:child_process module provides the ability to spawn subprocesses in a manner that is similar, but not identical, to popen(3) . This capability is primarily provided by the child_process. spawn() function: const { spawn } = require('node:child_process'); const ls = spawn('ls', ['-lh', '/usr']); ls. stdout.


1 Answers

The array ['--key ${BROWSERSTACK_KEY}'] passes a single command-line argument containing a space to the process. To pass two command-line arguments (what it probably expects), use two strings:

execFile('./BrowserStackLocal', ['--key', '${BROWSERSTACK_KEY}'], ...

I presume ${BROWSERSTACK_KEY} is just your placeholder in the question for the actual key...

like image 106
T.J. Crowder Avatar answered Oct 17 '22 06:10

T.J. Crowder