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

前端 未结 2 1345
醉酒成梦
醉酒成梦 2021-02-07 11:46

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

In both of the methods it prints to stderr.

2条回答
  •  灰色年华
    2021-02-07 12:34

    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.

提交回复
热议问题