Check out a free preview of the full Digging Into Node.js course

The "Console Error & Process stderr" Lesson is part of the full, Digging Into Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Kyle writes a message to console error and console log, which use two different streams of output. Then, the information from the standard out stream is redirected to demonstrate different facets of write streams.

Preview
Close

Transcript from the "Console Error & Process stderr" Lesson

[00:00:00]
>> Kyle Simpson: What about standard error? Because there's regular success output and then there's error output. How would we hook into that? Well you could say, process.standarderror.right, and send some stuff to that particular file descriptor. There's also another convenience helper, which for our purposes right now we'll just use, which is console.error.

[00:00:24]
>> Kyle Simpson: Okay, so console.log and console.error are gonna direct themselves to two different standard IO streams, one standard out and one standard error. Let's just run this program and see if we can tell any difference between the two. It doesn't really look any different does it? They both look like they're showing up as console logs.

[00:00:45]
That's because by default the shell environment that we're running in is gonna interpret standard out and standard error outputs both the same and just print them. But they are actually different and we can prove to ourselves that they're different by doing some file redirection, file descriptor redirection, some output redirection.

[00:01:06]
So I'm gonna try to run this again and this is, again, we mentioned at the outset that if you're running in a Windows environment, this may be slightly different for you. I think this part actually works the same in Windows, but just be aware there are sometimes nuance differences between a Linux or a Mac environment versus Windows.

[00:01:24]
But I wanna do a redirect, which is the greater than symbol. And I wanna basically redirect a file descriptor. So I wanna redirect the one file descriptor, which is standard out, and I wanna redirect it to, and this is definitely different from Windows. On a Linux or Mac system there's a thing called Devnull, which is basically a bit trashcan.

[00:01:44]
Anything that goes there just is completely thrown away. So, if I say one redirected to Devnull and we rerun the program, you'll notice that the hello world stuff didn't print out anymore, it's cuz we redirected the standard out stream to Devnull. If I were to run that again but change it to two, you'll notice that it's gonna print hello world and not the oops because now it's redirecting the standard error output.

[00:02:10]
This is critical to understand these basic ideas of file descriptors and standard out versus standard error. Because if you're going to write a command line shell tool, something like a replacement of a Bash script or a Perl script. One of the strong conventions is that you do regular debug output on the standard out, and you do any sort of error outputs on standard error.

[00:02:33]
And the reason is system monitoring tools will often hook into the standard error stream and write those to log files, for example. They may not write all the debug information that's coming out, but they want all the errors. So it's good for you to make sure, it's really important for you, if you're gonna do an error don't just do console.log because that's your habit.

[00:02:53]
Do something like console.errors so that it goes to the error stream, okay? And by the way, we can actually redirect both of them at the same time. We could redirect to the Devnull, and then we could, and I always seem to get this one, this syntax wrong. We're gonna redirect to to the address that we're redirecting, I'm sorry, we're gonna redirect one to the address that we're already redirecting to, and now both of them are gone.

[00:03:20]
So now both of them are getting sent to Devnull. Now you don't typically do that yourself on a command line, but this is the sort of thing that your system automation tools will often be doing when they run your programs, they'll be redirecting outputs to different log files and things like that, okay.

[00:03:36]
>> Kyle Simpson: All right, let's go back to our code. Now we are outputting to standard out and outputting to standard error, that's good. But I said there's three standard IO streams. The other of the standard IO streams is standard in, that's an input stream. And we could, in theory, access that stream directly.

[00:03:58]
We could say something like process.stdin.read. Cuz it's an input stream so we need to read from it rather than write to it. We can do that, we can call read and read a character, read a bite off of the standard end stream. But we're not gonna do that for now, we are gonna do standard input a little bit later.

[00:04:15]
But we're not gonna use the raw stream, and it's because standard in streams are much more finicky, they're much more quirky than the output streams. They are significantly affected by variances in your interactive shell. And there's a bunch of little quirks and weirdnesses about how it knows an end of input signal, how that gets sent in, and all of that other stuff.

[00:04:42]
And that's not code that we want to reinvent. So when we get to the point where we wanna use standard in, we're gonna use a package that already does that and it's called get standard in. It already handles all their weirdnesses in a platform agnostic way. We don't wanna reauthor that logic.

[00:04:58]
So just be aware that there is a process, .stdin, but we're not gonna use it for right now.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now