'Error: spawn ENOENT on Windows

I'm on node v4.4.0 and on Windows 10. I'm using bunyan to log my node application.

try {
    var fs = require('fs');
    var path = require('path');
    var spawn = require('child_process').spawn;
    var through = require('through');
} catch (err) {
    throw err;
}

var prettyStream = function () {
    // get the binary directory of bunyan
    var bin = path.resolve(path.dirname(require.resolve('bunyan')), '..', 'bin', 'bunyan');
    console.log(bin); // this outputs C:\www\nodeapp\src\node_modules\bunyan\bin\bunyan, the file does exist

    var stream = through(function write(data) {
        this.queue(data);
    }, function end() {
        this.queue(null);
    });

    // check if bin var is not empty and that the directory exists
    if (bin && fs.existsSync(bin)) {
        var formatter = spawn(bin, ['-o', 'short'], {
            stdio: [null, process.stdout, process.stderr]
        });
        // stream.pipe(formatter.stdin); // <- did this to debug
    }

    stream.pipe(process.stdout); // <- did this to debug

    return stream;
}

The logging spits out in the console due to the fact I used stream.pipe(process.stdout);, i did this to debug the rest of the function.

I however receive the error:

Error: spawn C:\www\nodeapp\src\node_modules\bunyan\bin\bunyan ENOENT
    at exports._errnoException (util.js:870:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32)
    at onErrorNT (internal/child_process.js:344:16)
    at nextTickCallbackWith2Args (node.js:442:9)
    at process._tickCallback (node.js:356:17)
    at Function.Module.runMain (module.js:443:11)
    at startup (node.js:139:18)
    at node.js:968:3

I'm guessing this is a Windows error. Anyone have any ideas?



Solution 1:[1]

I got it. On Windows bunyan isn't recognized in the console as a program but as a command. So to invoke it the use of cmd was needed. I also had to install bunyan globally so that the console could access it.

if (!/^win/.test(process.platform)) { // linux
    var sp = spawn('bunyan', ['-o', 'short'], {
        stdio: [null, process.stdout, process.stderr]
    });
} else { // windows
    var sp = spawn('cmd', ['/s', '/c', 'bunyan', '-o', 'short'], {
        stdio: [null, process.stdout, process.stderr]
    });
}

Solution 2:[2]

Use {shell: true} in the options of spawn

I was hit with this problem recently so decided to add my findings here. I finally found the simplest solution in the Node.js documentation. It explains that:

  • child_process.exec() runs with shell
  • child_process.execFile() runs without shell
  • child_process.spawn() runs without shell (by default)

This is actually why the exec and spawn behave differently. So to get all the shell commands and any executable files available in spawn, like in your regular shell, it's enough to run:

const { spawn } = require('child_process')
const myChildProc = spawn('my-command', ['my', 'args'], {shell: true})

or to have a universal statement for different operating systems you can use

const myChildProc = spawn('my-command', ['my', 'args'], {shell: process.platform == 'win32'})

Side notes:

  1. It migh make sense to use such a universal statement even if one primairly uses a non-Windows system in order to achieve full interoperability
  2. For full consistence of the Node.js child_process commands it would be helpful to have spawn (with shell) and spawnFile (without shell) to reflect exec and execFile and avoid this kind of confusions.

Solution 3:[3]

I solved same problem using cross-spawn. It allows me to spawn command on both windows and mac os as one common command.

Solution 4:[4]

I think you'll find that it simply can't find 'bunyun', but if you appended '.exe' it would work. Without using the shell, it is looking for an exact filename match to run the file itself.

When you use the shell option, it goes through matching executable extensions and finds a match that way. So, you can save some overhead by just appended the executable extension of your binary.

Solution 5:[5]

I was having this same problem when trying to execute a program in the current working directory in Windows. I solved it by passing the options { shell: true, cwd: __dirname } in the spawn() call. Then everything worked, with every argument passed as an array (not attached to the program name being run).

Solution 6:[6]

I think, the path of bin or something could be wrong. ENOENT = [E]rror [NO] [ENT]ry

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 basickarl
Solution 2 Jacek J
Solution 3 hshan
Solution 4 Ian
Solution 5 Anachronist
Solution 6 Tusk