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

302
Views
How to execute WSL commands from a node process?

I'm developing a small node build script that I want to launch WSL, cd to my project directory, and invoke a build command. Something like this:

import { exec } from "child_process";

async function execCommand(cmd) {
  console.log("Executing: " + cmd);

  return new Promise((resolve, reject) => {
    exec(cmd, (error, stdout, stderr) => {
      if (error) { reject(); }
      else { resolve(); }
    });
  });
}

await execCommand(`wsl`);
await execCommand(`cd /mnt/c/Users/Admin/Documents/Projects/myproject/backend`);
await execCommand(`cargo build --release`);

My issue is that it just.. doesn't work. That is, the first execCommand calls wsl and then the terminal window seems to never complete the command (it never finishes).

Instead, it prompts the user to type something in the command prompt.

What is the proper way to launch WSL from a node script and then chain a bunch of commands together, each one waiting on the previous one to finish?

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

0

Finally got it working.

Async version:

let child = spawn("wsl", [ "cd", "/mnt/c/Users/Admin/Documents/Projects/myserver/backend", "&&", "cargo", "build", "--release" ], {
  cwd: "C:\\Users\\Admin\\Documents\\Projects\\myserver\\backend",
  shell: true
});

child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);

child.stdin.end();

Synchronous version:

let child = spawnSync("wsl", [ "cd", "/mnt/c/Users/Admin/Documents/Projects/myserver/backend", "&&", "cargo", "build", "--release" ], {
  cwd: "C:\\Users\\Admin\\Documents\\Projects\\myserver\\backend",
  shell: true
});

console.log(child.stdout.toString());
console.log(child.stderr.toString());
about 4 years ago · Juan Pablo Isaza Report

0

Actually, my other answer isn't that good, because it launches a separate shell and runs the commands through the cmd shell instead, so the binary will be built by Windows instead of WSL, which is not what we want. A better answer is:

let child = spawnSync("wsl", [ "bash", "-l", "-c", "cargo build --release" ], {
  cwd: "C:\\Users\\Admin\\Documents\\Projects\\myserver\\backend"
});

console.log(child.stdout.toString());
console.log(child.stderr.toString());

You need to include bash -l because when you run a command through wsl it doesn't execute ~/.profile and ~/.bashrc (etc). Cargo needs that so it can insert ~/.cargo/bin to $PATH.

So with bash -l you force it to execute the profile init scripts. When you launch WSL without any other command, it will login interactively meaning those scripts are executed.

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!