Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
Nodejs: Exec from child_process returns bin/sh: 1: command not found but works when writing manually in terminal?

Using Node.js to automatically execute command in the terminal on raspberry pi, tho it wont work with exec(command). It outputs bin/sh: 1: command not found when trying to catch the output. But the command works when writing the command manually in the terminal?

Why is that?


async function run_command_fuel() {
    const command = "weconnect-cli PASSWORDINFORMATION get /vehicles/SECRETNUM/domains/fuelStatus/rangeStatus/primaryEngine/remainingRange_km";
   
    let returnval = 0;

    let child = exec(command);


    await new Promise((resolve, reject) => {
        child.stdout.on('data', function(data) {
            console.log('stdout: ' + data);
            returnval = data;
            console.log(returnval);
            resolve();
        });
        child.stderr.on('data', function(data) {
            console.log('stderr: ' + data);
            reject();
        });
        child.on('close', function(code) {
            console.log('closing code: ' + code);
        });
    })

    return returnval;

}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Running the exec command creates a new shell, which wont have the same environment as your node process. You will need to specify the PATH environment variable in order to use that command.

Node gives you a way to do this through the object you pass in the exec command, as the second parameter, which defaults to process.env.

To pass in the object:

const command = "weconnect-cli PASSWORDINFORMATION get /vehicles/SECRETNUM/domains/fuelStatus/rangeStatus/primaryEngine/remainingRange_km";

let child = exec(command, {env: {'PATH': 'path/to/command'}});

Another way you can do this is through standard shell env setting before a command. You can modify your command to this:

const env_vars = 'PATH=' + 'path/to/command' + ' ';
const command = "weconnect-cli PASSWORDINFORMATION get /vehicles/SECRETNUM/domains/fuelStatus/rangeStatus/primaryEngine/remainingRange_km";

let child = exec(env_vars + command);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!