Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between util.error and console.error in Node.js?

Tags:

node.js

What exactly is the difference between the util.error([...]) and console.error([...])?

In both of the methods it prints to stderr.

like image 494
Amol M Kulkarni Avatar asked Jan 21 '13 11:01

Amol M Kulkarni


1 Answers

Documentation

util.error

Same as util.debug() except this will output all arguments immediately to stderr.

util.debug

A synchronous output function. Will block the process and output string immediately to stderr.

console.error

Same as console.log but prints to stderr.

console.log

Prints to stdout with newline. This function can take multiple arguments in a printf()-like way. Example:

console.log('count: %d', count);

If formatting elements are not found in the first string then util.inspect is used on each argument. See util.format() for more information.

Conclusion

According to is node.js' console.log asynchronous? the console.log is asynchronous(node>=0.6), therefore also console.error. But util.error will block the process and output to stderr, according to the documentation above.

like image 191
Mattias Avatar answered Oct 26 '22 05:10

Mattias