1'use strict'; 2require('../common'); 3const tmpdir = require('../common/tmpdir'); 4const assert = require('assert'); 5const fs = require('fs'); 6const join = require('path').join; 7const { spawnSync } = require('child_process'); 8 9// Test that invoking node with require, and piping stderr to file, 10// does not result in exception, 11// see: https://github.com/nodejs/node/issues/11257 12 13tmpdir.refresh(); 14const fakeModulePath = join(tmpdir.path, 'batman.js'); 15const stderrOutputPath = join(tmpdir.path, 'stderr-output.txt'); 16// We need to redirect stderr to a file to produce #11257 17const stream = fs.createWriteStream(stderrOutputPath); 18 19// The error described in #11257 only happens when we require a 20// non-built-in module. 21fs.writeFileSync(fakeModulePath, '', 'utf8'); 22 23stream.on('open', () => { 24 spawnSync(process.execPath, { 25 input: `require("${fakeModulePath.replace(/\\/g, '/')}")`, 26 stdio: ['pipe', 'pipe', stream] 27 }); 28 const stderr = fs.readFileSync(stderrOutputPath, 'utf8').trim(); 29 assert.strictEqual( 30 stderr, 31 '', 32 `piping stderr to file should not result in exception: ${stderr}` 33 ); 34 stream.end(); 35 fs.unlinkSync(stderrOutputPath); 36 fs.unlinkSync(fakeModulePath); 37}); 38