88 ERR_INVALID_ARG_TYPE ,
99 ERR_OUT_OF_RANGE
1010} = require ( 'internal/errors' ) . codes ;
11- const fs = require ( 'fs' ) ;
1211const { Buffer } = require ( 'buffer' ) ;
1312const {
1413 copyObject,
@@ -18,6 +17,13 @@ const { Readable, Writable } = require('stream');
1817const { getPathFromURL } = require ( 'internal/url' ) ;
1918const util = require ( 'util' ) ;
2019
20+ let fs ;
21+ function lazyFs ( ) {
22+ if ( fs === undefined )
23+ fs = require ( 'fs' ) ;
24+ return fs ;
25+ }
26+
2127const kMinPoolSpace = 128 ;
2228
2329let pool ;
@@ -92,7 +98,7 @@ function ReadStream(path, options) {
9298util . inherits ( ReadStream , Readable ) ;
9399
94100ReadStream . prototype . open = function ( ) {
95- fs . open ( this . path , this . flags , this . mode , ( er , fd ) => {
101+ lazyFs ( ) . open ( this . path , this . flags , this . mode , ( er , fd ) => {
96102 if ( er ) {
97103 if ( this . autoClose ) {
98104 this . destroy ( ) ;
@@ -142,7 +148,7 @@ ReadStream.prototype._read = function(n) {
142148 return this . push ( null ) ;
143149
144150 // the actual read.
145- fs . read ( this . fd , pool , pool . used , toRead , this . pos , ( er , bytesRead ) => {
151+ lazyFs ( ) . read ( this . fd , pool , pool . used , toRead , this . pos , ( er , bytesRead ) => {
146152 if ( er ) {
147153 if ( this . autoClose ) {
148154 this . destroy ( ) ;
@@ -177,7 +183,7 @@ ReadStream.prototype._destroy = function(err, cb) {
177183} ;
178184
179185function closeFsStream ( stream , cb , err ) {
180- fs . close ( stream . fd , ( er ) => {
186+ lazyFs ( ) . close ( stream . fd , ( er ) => {
181187 er = er || err ;
182188 cb ( er ) ;
183189 stream . closed = true ;
@@ -242,7 +248,7 @@ WriteStream.prototype._final = function(callback) {
242248} ;
243249
244250WriteStream . prototype . open = function ( ) {
245- fs . open ( this . path , this . flags , this . mode , ( er , fd ) => {
251+ lazyFs ( ) . open ( this . path , this . flags , this . mode , ( er , fd ) => {
246252 if ( er ) {
247253 if ( this . autoClose ) {
248254 this . destroy ( ) ;
@@ -270,7 +276,7 @@ WriteStream.prototype._write = function(data, encoding, cb) {
270276 } ) ;
271277 }
272278
273- fs . write ( this . fd , data , 0 , data . length , this . pos , ( er , bytes ) => {
279+ lazyFs ( ) . write ( this . fd , data , 0 , data . length , this . pos , ( er , bytes ) => {
274280 if ( er ) {
275281 if ( this . autoClose ) {
276282 this . destroy ( ) ;
0 commit comments