Skip to content

Terminal colors built-in to Node #42770

Description

@Exortions

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_modules folder, 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:

console.log(`${Colors.PURPLE}${Colors.ITALIC}This is italicized purple text`

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.

Activity

  1. Trott commented on Apr 18, 2022

    @Trott
    Member

    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 is false.

  2. moved this to Pending Triage in Node.js feature requestson Apr 18, 2022
  3. BobFrankston commented on Apr 22, 2022

    @BobFrankston

    console.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.

  4. Trott commented on Apr 22, 2022

    @Trott
    Member

    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.

    image

    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 %c is 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.)

  5. BobFrankston commented on Apr 22, 2022

    @BobFrankston

    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.

  6. hemanth commented on May 2, 2022

    @hemanth
    Contributor

    util.inspect.formatText would be a good addition.

  7. ljharb commented on May 2, 2022

    @ljharb
    SponsorMember

    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.

  8. github-actions commented on Dec 7, 2022

    @github-actions
    Contributor

    There 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.

  9. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Dec 7, 2022
  10. github-actions commented on Jan 7, 2023

    @github-actions
    Contributor

    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.

  11. moved this from Pending Triage to Stale in Node.js feature requestson Apr 4, 2023
  12. vorant94 commented on Dec 24, 2024

    @vorant94

    for anyone passing by searching for some simple log coloring without the need to install 3rd party libs, there is styleText function from util.inspect that covered all my current needs

    https://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,
    );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions