Skip to content

Commit 621aaaf

Browse files
cjihrigandrew749
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: nodejs/node#10802 PR-URL: nodejs/node#10842 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Italo A. Casas <me@italoacasas.com>
1 parent 359de5c commit 621aaaf

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

doc/api/cli.md

+6
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@ added: v0.11.15
314314
Data path for ICU (Intl object) data. Will extend linked-in data when compiled
315315
with small-icu support.
316316

317+
### `NODE_NO_WARNINGS=1`
318+
<!-- YAML
319+
added: REPLACEME
320+
-->
321+
322+
When set to `1`, process warnings are silenced.
317323

318324
### `NODE_REPL_HISTORY=file`
319325
<!-- YAML

doc/node.1

+4
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ When set to 1 colors will not be used in the REPL.
218218
Data path for ICU (Intl object) data. Will extend linked-in data when compiled
219219
with small\-icu support.
220220

221+
.TP
222+
.BR NODE_NO_WARNINGS =\fI1\fR
223+
When set to \fI1\fR, process warnings are silenced.
224+
221225
.TP
222226
.BR NODE_REPL_HISTORY =\fIfile\fR
223227
Path to the file used to store the persistent REPL history. The default path

lib/internal/process/warning.js

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

1111
function setupProcessWarnings() {
12-
if (!process.noProcessWarnings) {
12+
if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') {
1313
process.on('warning', (warning) => {
1414
if (!(warning instanceof Error)) return;
1515
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)