Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of new line characters in an output of Node.js util.inspect?

An issue with Node.js' util.inspect function. I use it to colorize Node.js console output and to format objects. All works fine except that util.inspect prints all those \r\n characters too.

The example. Given a file:

// foo.js

console.log("css:\n", util.inspect(css, { depth: 5, colors: true }), '\n');

The output:

enter image description here

How to make util.inspect not to print all those \r\n characters?

like image 277
Green Avatar asked Oct 18 '22 19:10

Green


2 Answers

Looks like the solution suggested in the docs is to manually remove the new line characters:

util.inspect(css, { depth: 5, colors: true }).replace(/\r?\n/g, '')
like image 124
Jan Molak Avatar answered Nov 01 '22 10:11

Jan Molak


According to the current documentation of util.inspect, all you need to do is to set breakLength to Infinity and compact to true.

In your case that would be

console.log("css:\n", util.inspect(css, {
  depth: 5,
  colors: true,
  breakLength: Infinity,
  compact: true,
}), '\n');
like image 38
Diomidis Spinellis Avatar answered Nov 01 '22 09:11

Diomidis Spinellis