diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js index 2fc344765d8161..f372eef1613550 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js @@ -10,7 +10,7 @@ const { const { defaultGetFormat } = require('internal/modules/esm/get_format'); const { validateAttributes, emitImportAssertionWarning } = require('internal/modules/esm/assert'); const { getOptionValue } = require('internal/options'); -const { readFileSync } = require('fs'); +const fs = require('fs'); const defaultType = getOptionValue('--experimental-default-type'); @@ -38,7 +38,11 @@ function getSourceSync(url, context) { const responseURL = href; let source; if (protocol === 'file:') { - source = readFileSync(url); + // If you are reading this code to figure out how to patch Node.js module loading + // behavior - DO NOT depend on the patchability in new code: Node.js + // internals may stop going through the JavaScript fs module entirely. + // Prefer module.registerHooks() or other more formal fs hooks released in the future. + source = fs.readFileSync(url); } else if (protocol === 'data:') { const result = dataURLProcessor(url); if (result === 'failure') { diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 62b842d61cda63..8c917ad60eb8a5 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -25,7 +25,7 @@ const { const assert = require('internal/assert'); const internalFS = require('internal/fs/utils'); const { BuiltinModule } = require('internal/bootstrap/realm'); -const { realpathSync } = require('fs'); +const fs = require('fs'); const { getOptionValue } = require('internal/options'); // Do not eagerly grab .manifest, it may be in TDZ const { sep, posix: { relative: relativePosixPath }, resolve } = require('path'); @@ -277,7 +277,11 @@ function finalizeResolution(resolved, base, preserveSymlinks) { } if (!preserveSymlinks) { - const real = realpathSync(path, { + // If you are reading this code to figure out how to patch Node.js module loading + // behavior - DO NOT depend on the patchability in new code: Node.js + // internals may stop going through the JavaScript fs module entirely. + // Prefer module.registerHooks() or other more formal fs hooks released in the future. + const real = fs.realpathSync(path, { [internalFS.realpathCacheKey]: realpathCache, }); const { search, hash } = resolved; diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 9930957f4b3fa3..ba09085992ccb1 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -24,7 +24,7 @@ const { const { BuiltinModule } = require('internal/bootstrap/realm'); const assert = require('internal/assert'); -const { readFileSync } = require('fs'); +const fs = require('fs'); const { dirname, extname } = require('path'); const { assertBufferSource, @@ -357,7 +357,11 @@ translators.set('commonjs', function commonjsStrategy(url, translateContext, par try { // We still need to read the FS to detect the exports. - translateContext.source ??= readFileSync(new URL(url), 'utf8'); + // If you are reading this code to figure out how to patch Node.js module loading + // behavior - DO NOT depend on the patchability in new code: Node.js + // internals may stop going through the JavaScript fs module entirely. + // Prefer module.registerHooks() or other more formal fs hooks released in the future. + translateContext.source ??= fs.readFileSync(new URL(url), 'utf8'); } catch { // Continue regardless of error. }