Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

312
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda