From 08d61e594d3427a6442195856939df7a8930c918 Mon Sep 17 00:00:00 2001 From: Raphael Abayomi Date: Tue, 25 Aug 2026 09:28:31 +0100 Subject: [PATCH 1/2] Respect NO_COLOR environment variable in the dev format Fixes #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. --- index.js | 9 +++++++++ test/morgan.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/index.js b/index.js index 313f5dcb..7989f230 100644 --- a/index.js +++ b/index.js @@ -207,6 +207,15 @@ morgan.format('tiny', ':method :url :status :res[content-length] - :response-tim */ morgan.format('dev', function developmentFormatLine (tokens, req, res) { + // 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 = + compile(':method :url :status :response-time ms - :res[content-length]')) + + return plainFn(tokens, req, res) + } + // get the status code if response written var status = headersSent(res) ? res.statusCode diff --git a/test/morgan.js b/test/morgan.js index 85d3b9a7..e8f93bfc 100644 --- a/test/morgan.js +++ b/test/morgan.js @@ -1474,6 +1474,38 @@ describe('morgan()', function () { .expect(200, cb) }) }) + + describe('with NO_COLOR environment variable set', function () { + beforeEach(function () { + process.env.NO_COLOR = '1' + }) + + afterEach(function () { + delete process.env.NO_COLOR + }) + + it('should not emit any ANSI escape codes', function (done) { + var cb = after(2, function (err, res, line) { + if (err) return done(err) + assert.strictEqual(line.indexOf('\x1b'), -1) + assert.ok(/^GET \/ 200 \d+\.\d{3} ms - -$/.test(line), 'unexpected line: ' + line) + done() + }) + + var stream = createLineStream(function onLine (line) { + cb(null, null, line) + }) + + var server = createServer('dev', { stream: stream }, function (req, res, next) { + res.statusCode = 200 + next() + }) + + request(server) + .get('/') + .expect(200, cb) + }) + }) }) describe('short', function () { From b834227129a71c57bb8df8783235c8136aec75ce Mon Sep 17 00:00:00 2001 From: Ulises Gascon Date: Wed, 26 Aug 2026 18:03:30 +0200 Subject: [PATCH 2/2] docs: document NO_COLOR support in the dev format --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 5c61bc78..9f4e5ebd 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,10 @@ for information codes. GET /dev 200 0.224 ms - 2 ``` +Set the `NO_COLOR` environment variable to a non-empty value to disable the +colored output (see [no-color.org](https://no-color.org/)). The same fields are +then printed without any ANSI escape sequences. + ##### short Shorter than default, also including response time.