diff --git a/lib/internal/main/print_help.js b/lib/internal/main/print_help.js index de1ab43ce756b0..62269956be9faf 100644 --- a/lib/internal/main/print_help.js +++ b/lib/internal/main/print_help.js @@ -202,6 +202,7 @@ function format( function print(stream) { const { options, aliases } = getCLIOptionsInfo(); + const console = require('internal/console/global'); // Use 75 % of the available width, and at least 70 characters. const width = MathMax(70, (stream.columns || 0) * 0.75); @@ -212,21 +213,22 @@ function print(stream) { '(default if no file name is provided, ' + 'interactive mode if a tty)' }); options.set('--', { helpText: 'indicate the end of node options' }); - stream.write( + let helpText = ( 'Usage: node [options] [ script.js ] [arguments]\n' + ' node inspect [options] [ script.js | host:port ] [arguments]\n\n' + 'Options:\n'); - stream.write(indent(format({ + helpText += (indent(format({ options, aliases, firstColumn, secondColumn, }), 2)); - stream.write('\nEnvironment variables:\n'); + helpText += ('\nEnvironment variables:\n'); - stream.write(format({ + helpText += (format({ options: envVars, firstColumn, secondColumn, })); - stream.write('\nDocumentation can be found at https://nodejs.org/\n'); + helpText += ('\nDocumentation can be found at https://nodejs.org/'); + console.log(helpText); } prepareMainThreadExecution(); diff --git a/test/parallel/test-cli-node-print-help.js b/test/parallel/test-cli-node-print-help.js index 7e7c77f53e9657..8daf00278f8135 100644 --- a/test/parallel/test-cli-node-print-help.js +++ b/test/parallel/test-cli-node-print-help.js @@ -7,7 +7,8 @@ const common = require('../common'); // returns the proper set of cli options when invoked const assert = require('assert'); -const { exec } = require('child_process'); +const { exec, spawn } = require('child_process'); +const { once } = require('events'); let stdOut; @@ -53,3 +54,13 @@ function testForSubstring(options) { } startPrintHelpTest(); + +// Test closed stdout for `node --help`. Like `node --help | head -n5`. +(async () => { + const cp = spawn(process.execPath, ['--help'], { + stdio: ['inherit', 'pipe', 'inherit'], + }); + cp.stdout.destroy(); + const [exitCode] = await once(cp, 'exit'); + assert.strictEqual(exitCode, 0); +})().then(common.mustCall());