Repository navigation
Terminal colors built-in to Node #42770
Description
Activity
- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.
on Apr 18, 2022 I know this isn't what you're asking for exactly, but just in case anyone wants to be able to use these codes right now without a library:
List of built-in colors:
console.log(util.inspect.colors)
Note that despite it being named
colors, it contains non-color formatting too like italic. They are also in the docs.Creating/using ANSI codes from these:
const util = require('util') const formatTxt = (format, txt) => `\u001b[${format[0]}m${txt}\u001b[${format[1]}m`; const blue = util.inspect.colors.blue const italic = util.inspect.colors.italic console.log(formatTxt(blue, 'This is blue text')) console.log(formatTxt(italic, 'This is italic text')) console.log(formatTxt(blue, formatTxt(italic, 'This is blue, italic text')))
A robust library would also likely do things like check the return value of
tty.hasColors()and omit the ANSI codes if that isfalse.Reacted by Xuguang Mei, Willian Agostini, Quade Maiello, Alex Yang, Samad Rahimi and fuchsiaconsole.log already supports colors in Visual Studio Code with warnings and errors highlighted. There is also the %c which works in Visual Studio Code. Doing the same for console output in the command shell (terminal) would be valuable. At the very least, it would match the error handling capabilities found when running under VSC.
The problem with using escape sequences is that they often produce spurious output on devices without VT-100 emulation. It would be more consistent to share the same mechanism.
console.log already supports colors in Visual Studio Code with warnings and errors highlighted.
So does Node.js, although we could do more with color than we do perhaps. Right now, for example, in the following error, strings are highlighted in green and parts of the stack trace that are internal are slightly greyed out a bit, but that's it.
There is also the %c which works in Visual Studio Code. Doing the same for console output in the command shell (terminal) would be valuable.
That would require adding CSS support to Node.js, which is neither a small undertaking nor something very likely to happen in the near future. I suppose we could consider supporting a tiny subset of CSS, but I'm inclined to be skeptical of that approach. (UPDATE: I see that
%cis already limited to a subset of CSS. Perhaps this is more workable than I thought, although still very much not trivial I imagine. 🤔 Refs: https://developer.mozilla.org/en-US/docs/Web/API/console)The problem with using escape sequences is that they often produce spurious output on devices without VT-100 emulation. It would be more consistent to share the same mechanism.
Unless we're going to do what VS Code does and basically ship an entire Chromium browser along with Node.js, I'm not sure there's going to be any getting around those limitations. (Maybe I'm misunderstanding what you mean.)
Thanks, I see there is already some color capability for error messages. Yes, the subset for VSC is exactly what I want so that it works in both environments without having to depend on particular libraries. The goal is to be able to highlight some messages.
As you note, going further is a major step and not necessary for the modest purpose of highlighting messages.
util.inspect.formatTextwould be a good addition.cc @nodejs/util
It would be a worthy goal to eliminate the need for colors/chalk; i don’t think new general-purpose formatting APIs are particularly valuable.
Reacted by Hemanth HMThere has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.
For more information on how the project manages feature requests, please consult the feature request management document.
- addedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on Dec 7, 2022 There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment.
For more information on how the project manages feature requests, please consult the feature request management document.
for anyone passing by searching for some simple log coloring without the need to install 3rd party libs, there is
styleTextfunction fromutil.inspectthat covered all my current needshttps://nodejs.org/api/util.html#utilstyletextformat-text-options
import { styleText } from "node:util"; import { format } from "date-fns"; // ... console.debug( styleText("blue", `[${requestId}] -`), styleText("green", `${format(new Date(), "dd/MM/yyyy, HH:mm:ss")}\t`), styleText("gray", "DEBUG"), styleText("yellow", `[${name}]\t`), ...args, );
Reacted by Vladimir Starkov and Owen Buckley

What is the problem this feature will solve?
Having to download external libraries like chalk, colors, etc.
This also aids the scenario where programmers are without package managers. Sometimes, one standalone file in JavaScript is best. Implementing this feature would aid those individuals, and also reduce the bundle size because no package managers would have to be used for a simple JavaScript program.
Using Yarn or NPM makes a
node_modulesfolder, which is especially large by only adding one dependency just for terminal colors.Hopefully, you take this issue into consideration, and I would be happy to help implement a such feature.
What is the feature you are proposing to solve the problem?
An enumeration of ANSI escape codes for users to easily access to change the color of the terminal.
Example usage of this would be:
What alternatives have you considered?
One alternative (already exists) would be to use a package manager like Yarn or NPM to download a package like 'chalk' or 'colors'.
However, Node.JS users not using package managers would be unable to accomplish this.