Skip to content

console: add support for %c style formatter in console.log() #29605

Description

@humphd

Node's console currently supports a number of formatters, s, j, d, o, O, i, f. However, it doesn't support %c:

> console.log('%cHello World', 'color:green; background:yellow;')
%cHello World color:green; background:yellow;

The same code in Safari, Firefox, and Chrome does this:

Screen Shot 2019-09-18 at 3 55 58 PM

The Console spec doesn't give a ton of help on what %c should do, other than to say that it "Applies provided CSS."

Obviously there are aspects of this that don't make sense in the terminal vs. a browser, but perhaps some of it does? It would be nice if node's console could do more than ignore %c.

Apologies if this has come up in the past and been rejected. I wasn't able to find a previous discussion.

Activity

  1. added
    consoleIssues and PRs related to the console subsystem.
    feature requestIssues requesting new Node.js features.
    on Sep 18, 2019
  2. devsnek commented on Sep 18, 2019

    @devsnek
    Member

    Technically possible but we'd need to bring in a CSS parser, map all the ANSI stuff, etc...

    What if we just strip out %c formatting entirely:

    > console.log('%cHello World', 'color:green; background:yellow;')
    Hello World
    
  3. jasnell commented on Sep 18, 2019

    @jasnell
    Member

    For now, I'd say stripping out the %c formatting is the better approach.

  4. added a commit that references this issue on Sep 23, 2019
  5. added a commit that references this issue on Sep 25, 2019
  6. jcbhmr commented on Jul 15, 2023

    @jcbhmr
    Contributor

    Is there any interest in pursuing this to make CSS %c colors work in Node.js? Deno has this support, so it's already "been done" in the sense that there's public MIT-licensed code that could be used as a how-it-was-already-done example: https://github.com/denoland/deno/blob/306b51d7728a6e28461171d3332a0b3aa8545a6e/ext/console/01_console.js#L2821-L2926

    I think that what would need to change is here: https://github.com/nodejs/node/blob/main/lib/internal/util/inspect.js#L2263

  7. added a commit that references this issue on Jul 27, 2026
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

    consoleIssues and PRs related to the console subsystem.feature requestIssues requesting new Node.js features.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions