From 629ff27156f5ec40e8e8a85c2c3cdd3847c20723 Mon Sep 17 00:00:00 2001 From: armorbreak001 Date: Tue, 7 Jul 2026 12:52:12 +0800 Subject: [PATCH] fix: respect NO_COLOR environment variable in dev format When the NO_COLOR environment variable is set (per https://no-color.org/), the dev format now outputs plain text instead of ANSI-colored output. This improves accessibility for users with limited vision who may have difficulty reading colored terminal output. Fixes #302 --- index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 313f5dc..e011f21 100644 --- a/index.js +++ b/index.js @@ -212,6 +212,9 @@ morgan.format('dev', function developmentFormatLine (tokens, req, res) { ? res.statusCode : undefined + // check for NO_COLOR environment variable (https://no-color.org) + var noColor = process.env.NO_COLOR != null + // get status color var color = status >= 500 ? 31 // red : status >= 400 ? 33 // yellow @@ -220,12 +223,15 @@ morgan.format('dev', function developmentFormatLine (tokens, req, res) { : 0 // no color // get colored function - var fn = developmentFormatLine[color] + var fn = developmentFormatLine[color + (noColor ? '-nc' : '')] if (!fn) { // compile - fn = developmentFormatLine[color] = compile('\x1b[0m:method :url \x1b[' + - color + 'm:status\x1b[0m :response-time ms - :res[content-length]\x1b[0m') + fn = developmentFormatLine[color + (noColor ? '-nc' : '')] = compile( + noColor + ? ':method :url :status :response-time ms - :res[content-length]' + : '\x1b[0m:method :url \x1b[' + color + 'm:status\x1b[0m :response-time ms - :res[content-length]\x1b[0m' + ) } return fn(tokens, req, res)