Js child_process command not found

I have done the following two tests, to know the version of node I am using but I am getting the following errors.

Can you tell me where I’m wrong?

Test1:

import { exec as cExec } from "child_process";
import { promisify } from "util";
const exec = promisify(cExec);

    const cmd = `node -v`;

    const { stdout, stderr } = await exec(cmd);
    if (stderr) throw new Error(stderr);
    console.log(stdout);

Error1:

Error: Command failed: node -v
/bin/sh: node: command not found


/bin/sh: node: command not found
ChildProcess.exithandler:node:child_process:419:12
ChildProcess.emit:node:events:513:28

Test2:

const { spawn } = require('node:child_process');

    const ls = spawn('node', ['-v']);

    ls.stdout.on('data', (data) => {
      console.log(`stdout: ${data}`);
    });
    
    ls.stderr.on('data', (data) => {
      console.error(`stderr: ${data}`);
    });
    
    ls.on('close', (code) => {
      console.log(`child process exited with code ${code}`);
    });

Error2:

Error: spawn node ENOENT

ChildProcess._handle.onexit:child_process:283:19
onErrorNT:child_process:476:16

  • The node command is apparently not in your PATH. Try using the full path to node.

    – 

  • If I do it from the terminal it works.

    – 

  • You may have different $PATH setting.

    – 

  • What does type node show in the terminal?

    – 

Leave a Comment