diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 3272608bf7d04d..7f2582698dce21 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -43,7 +43,7 @@ const { aggregateTwoErrors, } = require('internal/errors'); const { isArrayBufferView } = require('internal/util/types'); -const { rimrafPromises } = require('internal/fs/rimraf'); + const { constants: { kIoMaxLength, @@ -93,6 +93,7 @@ const { kEmptyObject, lazyDOMException, promisify, + getLazy, } = require('internal/util'); const EventEmitter = require('events'); const { StringDecoder } = require('string_decoder'); @@ -136,6 +137,8 @@ function lazyFsStreams() { return fsStreams ??= require('internal/fs/streams'); } +const lazyRimRaf = getLazy(() => require('internal/fs/rimraf').rimrafPromises); + // By the time the C++ land creates an error for a promise rejection (likely from a // libuv callback), there is already no JS frames on the stack. So we need to // wait until V8 resumes execution back to JS land before we have enough information @@ -804,7 +807,7 @@ async function ftruncate(handle, len = 0) { async function rm(path, options) { path = pathModule.toNamespacedPath(getValidatedPath(path)); options = await validateRmOptionsPromise(path, options, false); - return rimrafPromises(path, options); + return lazyRimRaf()(path, options); } async function rmdir(path, options) { @@ -815,7 +818,7 @@ async function rmdir(path, options) { emitRecursiveRmdirWarning(); const stats = await stat(path); if (stats.isDirectory()) { - return rimrafPromises(path, options); + return lazyRimRaf()(path, options); } }