Skip to content

Respect NO_COLOR environment variable in the dev format - #372

Closed
raphyabak wants to merge 3 commits into
expressjs:masterfrom
raphyabak:feat/respect-no-color
Closed

Respect NO_COLOR environment variable in the dev format#372
raphyabak wants to merge 3 commits into
expressjs:masterfrom
raphyabak:feat/respect-no-color

Conversation

@raphyabak

Copy link
Copy Markdown

Summary

Fixes #302.

morgan('dev') always emits ANSI color escape codes with no way to turn them off, even though NO_COLOR is the widely adopted convention for this, and colored escape codes mixed into logs can be a real accessibility problem for some terminal/screen-reader setups.

Fix

When NO_COLOR is set to a non-empty value, dev compiles and uses a plain, escape-code-free variant of the format line instead of the colored one (:method :url :status :response-time ms - :res[content-length], i.e. the same fields the colored line already prints, just without the \x1b[...] codes). It's cached the same way the colored variants already are, so there's no extra compile cost per request.

No other formats are affected — dev is the only built-in format that emits color.

Test plan

  • Added a test under the existing dev describe block: with NO_COLOR=1 set, asserts the logged line contains no \x1b escape character and matches the expected plain fields.
  • Verified the regression: reverting the source change turns the new test from passing into failing (indexOf('\x1b') returns 0 instead of -1, i.e. an escape code is present at the start of the line).
  • Full suite passes: npx mocha --check-leaks --reporter spec — 89 passing.
  • npm run lint (eslint) passes on the changed files.

raphyabak and others added 2 commits August 25, 2026 09:28
Fixes expressjs#302.

morgan('dev') always emitted ANSI color codes, with no way to disable
them, even though NO_COLOR (https://no-color.org/) is the widely
adopted convention for this and colored escape codes in logs can be a
real accessibility problem.

When NO_COLOR is set to a non-empty value, compile and use a plain,
escape-code-free variant of the dev format line instead of the colored
one, caching it the same way the colored variants are already cached.
@UlisesGascon UlisesGascon self-assigned this Aug 26, 2026

@UlisesGascon UlisesGascon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MaddyGuthridge MaddyGuthridge left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good to me. The design issues I spot with it are just continuations of the existing (imo questionable) design that already exists in the package, and so it's probably better to fix those in a different PR if we can be bothered.

Comment thread index.js
Comment on lines +217 to +218
var plainFn = developmentFormatLine.plain || (developmentFormatLine.plain =
compile(':method :url :status :response-time ms - :res[content-length]'))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it does match the behaviour elsewhere in this function, adding attributes to the function seems like an extremely strange design decision. It's not type-safe, and makes it quite unclear where the definitions actually come from.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it's house style and not worth a rewrite for this, but these are internal if that makes you feel any better about the type safety of it

Comment thread index.js
// NO_COLOR (https://no-color.org/): when present and not an empty
// string, regardless of its value, ANSI color codes must not be added
if (process.env.NO_COLOR) {
var plainFn = developmentFormatLine.plain || (developmentFormatLine.plain =

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nullish coalescing operator would be preferable for the expression too, although I don't think it is supported by versions of Node as old as what Morgan targets.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, cannot use ?? in this release line

@jonchurch

Copy link
Copy Markdown
Member

Im going to supersede this PR with my own in #377

@jonchurch jonchurch closed this Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

morgan('dev') does not respect NO_COLOR=1 environment variable

4 participants