Skip to content

fix: respect NO_COLOR environment variable in dev format#341

Open
mahmoodhamdi wants to merge 1 commit intoexpressjs:masterfrom
mahmoodhamdi:fix/respect-no-color
Open

fix: respect NO_COLOR environment variable in dev format#341
mahmoodhamdi wants to merge 1 commit intoexpressjs:masterfrom
mahmoodhamdi:fix/respect-no-color

Conversation

@mahmoodhamdi
Copy link
Copy Markdown

Summary

The dev format now respects the NO_COLOR environment variable by omitting ANSI escape codes when it is set.

Problem

The dev format uses ANSI color codes to colorize HTTP status codes (green for 2xx, cyan for 3xx, yellow for 4xx, red for 5xx). However, it does not respect the NO_COLOR environment variable, which is the widely-accepted standard for disabling color output in terminal applications.

This causes accessibility issues for users with limited vision and problems in environments where ANSI escape codes are not supported or cause garbled output (CI logs, file redirection, etc.).

Fix

When NO_COLOR is set in process.env, the dev format outputs plain text without any ANSI escape sequences:

GET / 200 2.341 ms - 13

Instead of:

\x1b[0mGET / \x1b[32m200\x1b[0m 2.341 ms - 13\x1b[0m

The no-color format is cached separately from colored formats, so there is no performance impact.

Testing

Added a test that verifies the output contains no ANSI escape codes when NO_COLOR is set. All 83 tests pass. Lint clean.

Fixes #302

The dev format uses ANSI color codes to colorize status codes, but
did not respect the NO_COLOR environment variable, which is the
widely-accepted standard for disabling color output in CLI tools
(see https://no-color.org).

When NO_COLOR is set, the dev format now outputs plain text without
any ANSI escape sequences, improving accessibility for users with
limited vision or environments where color codes cause issues.

Fixes expressjs#302
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant