Skip to content

A11y: support NO_COLOR environment variable in output - #303

Closed
MaddyGuthridge wants to merge 2 commits into
expressjs:masterfrom
MaddyGuthridge:maddy-implement-no-color
Closed

A11y: support NO_COLOR environment variable in output#303
MaddyGuthridge wants to merge 2 commits into
expressjs:masterfrom
MaddyGuthridge:maddy-implement-no-color

Conversation

@MaddyGuthridge

@MaddyGuthridge MaddyGuthridge commented May 29, 2025

Copy link
Copy Markdown

Resolves #302

This is a quick fix, rather than a major change to the system Morgan uses for styled console output. In an ideal world, using NO_COLOR would disable all styling of output, which would remove the presence of control characters in things like CI logs. Currently, this PR just sets the color to 0 if NO_COLOR is set in process.env, which is acceptable for accessibility.

As a side note: I'm not used to writing code for such old versions of Node, so apologies if I made any mistakes that break that compatibility. I tried testing using Node 0.8.28, but was unable to get npm i to work correctly. Let me know if I broke anything and I'll do my best to fix it up :)

  • Implementation
  • Test suite passes with 100% coverage
  • Linting passes with no errors or warnings

I accept the certificate of origin, and am happy for my contribution to use the project's MIT software license.

@krzysdz krzysdz 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.

I believe that NO_COLOR=1 should entirely remove the colour-related ANSI escape codes instead of emitting the reset sequence.

@MaddyGuthridge

Copy link
Copy Markdown
Author

Makes sense to me, although that would require a bit of a bigger refactor. I am pretty flat-out right now so probably won't have time to sort that out for a few weeks at least. I'll keep it on my todo list though.

@krzysdz

krzysdz commented Aug 20, 2026

Copy link
Copy Markdown

I have an idea that may be a little weird, but should work - after the current 'dev' format (in line 233) add something like

/**
 * NO_COLOR override of dev
 */

if (process.env.NO_COLOR) {
  morgan.format('dev', ':method :url :status :response-time ms - :res[content-length]')
}

It assumes that the NO_COLOR env var will be set before requiring morgan, which should be the case most of the time (this may cause problems with tests, as the relevant tests would have to import morgan separately). This should work, because morgan.format() performs a simple assignment, so calling it with the same name overwrites the previous format (this causes some problems as .format() and .token() can overwrite all morgan properties - #265):

morgan/index.js

Lines 480 to 491 in bd171d7

/**
* Define a format with the given name.
*
* @param {string} name
* @param {string|function} fmt
* @public
*/
function format (name, fmt) {
morgan[name] = fmt
return this
}

Alternatively, the developmentFormatLine and the string with no escape sequences could be stored as variables and set as the dev format when instantiating morgan somewhere above

morgan/index.js

Lines 107 to 110 in bd171d7

// format function
var formatLine = typeof fmt !== 'function'
? getFormatFunction(fmt)
: fmt

This would make testing easier, but IMO would negatively impact the code structure (suddenly one format is defined inside morgan(), not with other formats, and uses separate variables).

@jonchurch @UlisesGascon do you think this would be an acceptable solution?

@UlisesGascon

Copy link
Copy Markdown
Member

Based on #303 (comment), I think that #372 supperseeds this pr 🤔

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

3 participants