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!
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.
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With