Search code examples
javascriptnode.jsbrowserstack

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!


Solution

  • 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...