Search code examples
node.jsexecchild-processspawn

'child_process'.exec vs .spawn ENOENT


I do understand the difference of concept between .exec and .spawn. I am trying to run a simple command, lets say echo.

With .exec, the command does work as expected. With .spawn I receive Error: spawn echo ENOENT.

What am I doing incorrectly?

// WORKS AS EXEPCTED
const exec= require('child_process').exec;
exec("echo hello", function(err, stdout) {
    console.log(stdout);
});

// THROWS
const spawn = require('child_process').spawn;
spawn("echo", ["hello"]); // fail

Error received :

Error: spawn echo ENOENT
at exports._errnoException (util.js:1026:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:359:16)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
at Module.runMain (module.js:606:11)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3

I am running on Windows!

Thanks for your help and patience.


Solution

  • I finally find the answer to my question. The problem is that the spawn implementation on Windows can only start executables.

    To avoid this problem, you can use win-spawn.