NodeJS:扔掉; //使用child_process spawn方法时未处理的'error'事件(events.js:72)

时间:2021-05-24 00:07:55

I've made an node.js app to list all .txt files from a directory recursively and, for each one, do some stuff.

我已经创建了一个node.js应用程序,以递归方式列出目录中的所有.txt文件,并为每个文件执行一些操作。

Here's my app.js:

这是我的app.js:

var spawn = require('child_process').spawn,
    dir = spawn('dir', ['*.txt', '/b']);

dir.stdout.on('data', function (data) {
    //do some stuff with each stdout line...
    console.log('stdout: ' + data);
});

dir.stderr.on('data', function (data) {
    //throw errors
    console.log('stderr: ' + data);
});

dir.on('close', function (code) {
    console.log('child process exited with code ' + code);
});

When I run node app.js via console, I get the error message below:

当我通过控制台运行节点app.js时,我收到以下错误消息:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: spawn ENOENT
    at errnoException (child_process.js:980:11)
    at Process.ChildProcess._handle.onexit (child_process.js:771:34)

I'm using node v0.10.13 at win32 environment.

我在win32环境中使用节点v0.10.13。

I do this way (spawn) because I want to handle stdout line by line (the exec method release entire stdout as one string).

我这样做(spawn)因为我想逐行处理stdout(exec方法将整个stdout释放为一个字符串)。

* UPDATE *

*更新*

By the way, using spawn for child_process does not guarantee that the output for cmd dir will be line by line. I've created a question for that too.

顺便说一下,使用spawn for child_process并不能保证cmd dir的输出是逐行的。我也为此创建了一个问题。

2 个解决方案

#1


18  

That happen because dir is not a executable in Windows. It's a command from the shell.
The solution for your problem is the following:

这是因为dir不是Windows中的可执行文件。这是来自shell的命令。您的问题的解决方案如下:

var dir = spawn('cmd', ['/c', 'dir']);
dir.stdout.on("data", function() {
    // do things
})

This exact problem was here also.

这个确切的问题也在这里。

#2


12  

Several things:

几件事:

  • dir is not a real executable in windows, so node.js cannot find the program you want to run.
  • dir不是Windows中的真正可执行文件,因此node.js找不到您要运行的程序。
  • You didn't bind an 'error' handler to your child process and the event was turned into an exception that crashed your node instance. Do this:
  • 您没有将“错误”处理程序绑定到子进程,并且该事件变为导致节点实例崩溃的异常。做这个:
dir.on('error', function (err) {
  console.log('dir error', err);
});
  • Use fs.readdir instead. It's a standard node.js API that does the same thing.
  • 请改用fs.readdir。它是一个标准的node.js API,它做同样的事情。

#1


18  

That happen because dir is not a executable in Windows. It's a command from the shell.
The solution for your problem is the following:

这是因为dir不是Windows中的可执行文件。这是来自shell的命令。您的问题的解决方案如下:

var dir = spawn('cmd', ['/c', 'dir']);
dir.stdout.on("data", function() {
    // do things
})

This exact problem was here also.

这个确切的问题也在这里。

#2


12  

Several things:

几件事:

  • dir is not a real executable in windows, so node.js cannot find the program you want to run.
  • dir不是Windows中的真正可执行文件,因此node.js找不到您要运行的程序。
  • You didn't bind an 'error' handler to your child process and the event was turned into an exception that crashed your node instance. Do this:
  • 您没有将“错误”处理程序绑定到子进程,并且该事件变为导致节点实例崩溃的异常。做这个:
dir.on('error', function (err) {
  console.log('dir error', err);
});
  • Use fs.readdir instead. It's a standard node.js API that does the same thing.
  • 请改用fs.readdir。它是一个标准的node.js API,它做同样的事情。