Home > Software engineering >  Node.JS Child Process Change Type of Terminal
Node.JS Child Process Change Type of Terminal

Time:05-02

I'm using the Node JS child process to execute terminal commands and get the output of them, which is all going great. However, I'm on a WSL Windows machine, and I would like to change the type of the child processes terminal to zsh.

Below is my current code for executing a command and receiving the output.

async function execute(
    command: string,
    cwd: string,
): Promise<{ output: string; error: string }> {
    const out = await promisify(exec)(command, { cwd: cwd });

    const output = out.stdout.trim();
    const error = out.stderr.trim();

    return { output: output, error: error };
}

How can I change the type of the terminal from command prompt to zsh?

CodePudding user response:

you can do this

exec(command , { shell : "zsh" })

you can read more about shell requirements here : https://nodejs.org/api/child_process.html#shell-requirements

So your code should look like this :

async function execute(
    command: string,
    cwd: string,
): Promise<{ output: string; error: string }> {
    const out = await promisify(exec)(command, { cwd: cwd , 
    shell:"zsh"});

    const output = out.stdout.trim();
    const error = out.stderr.trim();

    return { output: output, error: error };
}

  • Related