Skip to content

Commit d24491c

Browse files
cjihrigitaloacasas
authored andcommitted
process: add NODE_NO_WARNINGS environment variable
This commit adds support for a NODE_NO_WARNINGS environment variable, which duplicates the functionality of the --no-warnings command line flag. Fixes: #10802 PR-URL: #10842 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Italo A. Casas <[email protected]>
1 parent aa8771f commit d24491c

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

doc/api/cli.md

+7
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,13 @@ added: v0.11.15
287287
Data path for ICU (Intl object) data. Will extend linked-in data when compiled
288288
with small-icu support.
289289

290+
### `NODE_NO_WARNINGS=1`
291+
<!-- YAML
292+
added: REPLACEME
293+
-->
294+
295+
When set to `1`, process warnings are silenced.
296+
290297
### `NODE_PRESERVE_SYMLINKS=1`
291298
<!-- YAML
292299
added: v7.1.0

doc/node.1

+4
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ errors are otherwise ignored.
198198
Data path for ICU (Intl object) data. Will extend linked-in data when compiled
199199
with small\-icu support.
200200

201+
.TP
202+
.BR NODE_NO_WARNINGS =\fI1\fR
203+
When set to \fI1\fR, process warnings are silenced.
204+
201205
.TP
202206
.BR NODE_PATH =\fIpath\fR[:\fI...\fR]
203207
\':\'\-separated list of directories prefixed to the module search path.

lib/internal/process/warning.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const prefix = `(${process.release.name}:${process.pid}) `;
55
exports.setup = setupProcessWarnings;
66

77
function setupProcessWarnings() {
8-
if (!process.noProcessWarnings) {
8+
if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') {
99
process.on('warning', (warning) => {
1010
if (!(warning instanceof Error)) return;
1111
const isDeprecation = warning.name === 'DeprecationWarning';
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cp = require('child_process');
5+
6+
if (process.argv[2] === 'child') {
7+
process.emitWarning('foo');
8+
} else {
9+
function test(env) {
10+
const cmd = `${process.execPath} ${__filename} child`;
11+
12+
cp.exec(cmd, { env }, common.mustCall((err, stdout, stderr) => {
13+
assert.strictEqual(err, null);
14+
assert.strictEqual(stdout, '');
15+
16+
if (env.NODE_NO_WARNINGS === '1')
17+
assert.strictEqual(stderr, '');
18+
else
19+
assert(/Warning: foo$/.test(stderr.trim()));
20+
}));
21+
}
22+
23+
test({});
24+
test(process.env);
25+
test({ NODE_NO_WARNINGS: undefined });
26+
test({ NODE_NO_WARNINGS: null });
27+
test({ NODE_NO_WARNINGS: 'foo' });
28+
test({ NODE_NO_WARNINGS: true });
29+
test({ NODE_NO_WARNINGS: false });
30+
test({ NODE_NO_WARNINGS: {} });
31+
test({ NODE_NO_WARNINGS: [] });
32+
test({ NODE_NO_WARNINGS: function() {} });
33+
test({ NODE_NO_WARNINGS: 0 });
34+
test({ NODE_NO_WARNINGS: -1 });
35+
test({ NODE_NO_WARNINGS: '0' });
36+
test({ NODE_NO_WARNINGS: '01' });
37+
test({ NODE_NO_WARNINGS: '2' });
38+
// Don't test the number 1 because it will come through as a string in the
39+
// the child process environment.
40+
test({ NODE_NO_WARNINGS: '1' });
41+
}

0 commit comments

Comments
 (0)