diff --git a/lib/internal/fs.js b/lib/internal/fs.js index 881679f99c4000..3b2ced58806e5f 100644 --- a/lib/internal/fs.js +++ b/lib/internal/fs.js @@ -2,7 +2,6 @@ const Buffer = require('buffer').Buffer; const Writable = require('stream').Writable; -const fs = require('fs'); const util = require('util'); const constants = process.binding('constants').fs; @@ -15,6 +14,15 @@ const O_SYNC = constants.O_SYNC | 0; const O_TRUNC = constants.O_TRUNC | 0; const O_WRONLY = constants.O_WRONLY | 0; +let fs; +// Lazy load fs due to a circular dependency. +function lazyFS() { + if (!fs) { + fs = require('fs'); + } + return fs; +} + function assertEncoding(encoding) { if (encoding && !Buffer.isEncoding(encoding)) { throw new Error(`Unknown encoding: ${encoding}`); @@ -72,7 +80,7 @@ function SyncWriteStream(fd, options) { util.inherits(SyncWriteStream, Writable); SyncWriteStream.prototype._write = function(chunk, encoding, cb) { - fs.writeSync(this.fd, chunk, 0, chunk.length); + lazyFS().writeSync(this.fd, chunk, 0, chunk.length); cb(); return true; }; @@ -82,7 +90,7 @@ SyncWriteStream.prototype._destroy = function() { return; if (this.autoClose) - fs.closeSync(this.fd); + lazyFS().closeSync(this.fd); this.fd = null; return true; diff --git a/test/parallel/test-stdin-require.js b/test/parallel/test-stdin-require.js new file mode 100644 index 00000000000000..593f619b6146fa --- /dev/null +++ b/test/parallel/test-stdin-require.js @@ -0,0 +1,20 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const execSync = require('child_process').execSync; +const fs = require('fs'); +const path = require('path'); + +common.refreshTmpDir(); +const filename = path.join(__dirname, '..', 'fixtures', 'baz.js'); +const out = path.join(common.tmpDir, 'js.out'); +const bin = process.execPath; +const input = `require('${filename}'); console.log('PASS');`; +const cmd = common.isWindows ? + `echo "${input}" | ${bin} > ${out} 2>&1; more ${out}` : + `echo "${input}" | ${bin} &> ${out}; cat ${out}`; + +const result = execSync(cmd).toString(); +assert.strictEqual(result.trim(), 'PASS'); +fs.unlinkSync(out);