NodeJS: throw er; //Unhandled 'error' event (events.js:72) when using child_process spawn method - node.js

I've made an node.js app to list all .txt files from a directory recursively and, for each one, do some stuff.
Here's my 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:
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.
I do this way (spawn) because I want to handle stdout line by line (the exec method release entire stdout as one string).
* 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.

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:
var dir = spawn('cmd', ['/c', 'dir']);
dir.stdout.on("data", function() {
// do things
})
This exact problem was here also.

Several things:
dir is not a real executable in windows, so node.js cannot find the program you want to run.
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.

Related

Why does not a try-catch block catch a child_process spawn exception due to invalid executable path in Node.js?

const { spawn } = require("child_process")
try{
spawn("invalid/path/to/executable")
}catch(err){
console.log("exception: ",err)
}
This code raises an error and the execution of the program stops. It never prints exception: so the catch block is not executed:
events.js:183
throw er; // Unhandled 'error' event
^
Error: spawn invalid/path/to/executable ENOENT
When run with a valid path to an executable, the same code works.
What can I do to handle the case when the spawn fails due to ENOENT error?
This module fires error event and you can just add a listener for it.
You can read more about it here
So, you can transform your code to:
const {spawn} = require("child_process")
const subprocess = spawn("invalid/path/to/executable")
subprocess.on('error', function (err) {
console.log('Failed to start subprocess: ' + err);
});
Also, I suggest reading this article by Samer Buna. He covered a lot of interesting topics about this module.

How to catch an ENOENT with nodejs child_process.fork? [duplicate]

How do I catch .fork() errors that call files that don't exist?
var cp = require('child_process');
var fk = cp.fork("missing-file.js");
spews out
module.js:340
throw err;
^
Error: Cannot find module 'path-to-here/missing-file.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
I've tried
try {
var cp = require('child_process');
var fk = cp.fork("missing-file.js");
} catch(err) {
console.log("async error doesn't get caught here.");
}
fk.on('error', function(err) {
console.log("this never gets called");
})
process.on('uncaughtException', function(err) {
console.log("this never gets called either");
});
but none of those catch the error.
Joyent's documentation says an error event should be emitted when:
The process could not be spawned, or
The process could not be killed, or
Sending a message to the child process failed for whatever reason.
But this appears to happen prior to #1.
I looked at Handle errors thrown by require() module in node.js but the solution there doesn't work.
How do I catch this error?
There is no error here. node started just fine, failed to find the file, and then exited. None of these things will throw an error in the parent process. However, the second step ("failed to find the file") caused the child process to emit some text on its stdout, which by default was inherited from the parent process. That's the source of the text you're seeing (to suppress it, pass fork the silent: true option).
If you're trying to detect this error, you can put a handler on the close event. That handler will be called with 2 arguments, but you only care about the 1st one: the exit code. Node uses exit code 8 when it can't find the source file (although note that a script can also use exit code 8, so this isn't bullet-proof). Note that exit code 0 conventionally means the process ended successfully.
So, if you want to act on the file not being found and suppress the error message from going to stdout, you can:
var cp = require('child_process');
var fk = cp.fork("missing-file.js", {silent: true});
fk.on('close', function(code) {
if(code == 8) {
// Handle the missing file case here.
}
})

Open apps using node.js spawn

I'm trying to do a little application with node.js that would run on mac and execute some commands.
I've successfully used spawn to run command lines such as xcodebuild, but xcrun doesn't seems to work when I try to open the iOS Simulator.
I can open on terminal by typing:
xcrun instruments -w 'iPhone 5s (9.2)' -t <template>
But if I use node and try to use spawn like this:
var args = ['instruments', '-w', `iPhone 5s (9.2)`, '-t', 'noTemp'];
var xcrun = spawn('xcrun', args);
So it got me thinking that maybe it had some limitation opening apps? I tried to run:
var args = ['/Applications/Spotify.app'];
var xcrun = spawn('open', args);
And nothing happens. I couldn't find anything related to that. My question is: is there anyway to open apps using node.js spawn? If there is, does someone know what's the problem with my code?
Here's the full code if needed:
var args = ['instruments', '-w', `${fullDevice}`, '-t', 'noTemp'];
var xcrun = spawn('xcrun', args);
xcrun.stdout.on('data', (data)=>{
console.log(data.toString('utf8'));
})
xcrun.on('close', (code) => {
socket.emit({
time: commands.getCurrentTime(),
type: 'success',
log: 'Device booted...'
});
callback();
if (code !== 0) {
console.log(`open process exited with code ${code}`);
}
});
OBS: if I run this piece of code the application doesn't terminate, the program doesn't continue and nothing happens.
EDIT: Changed:
xcrun.on('data', (data)=>{
To:
xcrun.stdout.on('data', (data)=>{
Spawned processes have two separate streams for stdout and stderr, so you will need to listen for data on those objects and not the spawned process object itself:
xcrun.stdout.on('data', function(data) {
console.log('stdout: ' + data.toString());
});
xcrun.stderr.on('data', function(data) {
console.log('stderr: ' + data.toString());
});
The problem was one line above. Not sure why, but there's a socket.emit call that is wrong and actually hold the program's execution.

Node.js run a java program?

I have a simple nodejs server up and running. I am running into trouble when I try to spawn a child process. the jar file I am trying to access, is in the same directory as the node script. It takes in a command line argument that I am trying to pass in and it outputs data to the command line, which I am trying to pipe into the var child.
var child = require('child_process').spawn('java -jar done.jar',['argument to pass in']);
child.stdout.on('data', function(data) {
console.log(data.toString());
});
child.stderr.on("data", function (data) {
console.log(data.toString());
});
This produces the following error message:
events.js:85
throw er; // Unhandled 'error' event
^
Error: spawn java -jar done.jar ENOENT
at exports._errnoException (util.js:746:11)
at Process.ChildProcess._handle.onexit (child_process.js:1046:32)
at child_process.js:1137:20
at process._tickCallback (node.js:355:11)
Any ideas?
You're executing java. -jar and done.jar are arguments to pass.
var child = require('child_process').spawn(
'java', ['-jar', 'done.jar', 'argument to pass in']
);
You will also need to specify the full path to java, or make sure that java is specified in the OS path.

How to respond to a command line promt with node.js

How would I respond to a command line prompt programmatically with node.js? For example, if I do process.stdin.write('sudo ls'); The command line will prompt for a password. Is there an event for 'prompt?'
Also, how do I know when something like process.stdin.write('npm install') is complete?
I'd like to use this to make file edits (needed to stage my app), deploy to my server, and reverse those file edits (needed for eventually deploying to production).
Any help would rock!
You'll want to use child_process.exec() to do this rather than writing the command to stdin.
var sys = require('sys'),
exec = require('child_process').exec;
// execute the 'sudo ls' command with a callback function
exec('sudo ls', function(error, stdout, stderr){
if (!error) {
// print the output
sys.puts(stdout);
} else {
// handle error
}
});
For the npm install one you might be better off with child_process.spawn() which will let you attach an event listener to run when the process exits. You could do the following:
var spawn = require('child_process').spawn;
// run 'npm' command with argument 'install'
// storing the process in variable npmInstall
var npmInstall = spawn('npm', ['install'], {
cwd: process.cwd(),
stdio: 'inherit'
});
// listen for the 'exit' event
// which fires when the process exits
npmInstall.on('exit', function(code, signal) {
if (code === 0) {
// process completed successfully
} else {
// handle error
}
});

Resources