错误:在Windows上产生ENOENT

我在节点v4.4.0和Windows 10.我使用bunyan来logging我的节点应用程序。

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; } 

由于我使用了stream.pipe(process.stdout);所以日志logging在控制台中吐出stream.pipe(process.stdout); ,我做了这个debugging其余的function。

我但是收到错误:

 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 

我猜这是一个Windows错误。 有人有主意吗?

我知道了。 在Windows上,bunyan在控制台中不能被识别为一个程序,而是一个命令。 所以要调用它,需要使用cmd 。 我还必须在全球安装bunyan,以便控制台可以访问它。

 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] }); } 

我用cross-spawn解决了同样的问题。 它允许我在Windows和Mac OS上产生命令作为一个常用的命令。

我认为, bin或其他东西可能是错的。 ENOENT = [E] rror [NO] [ENT] ry