@@ -27,8 +27,6 @@ const internalFS = require('internal/fs/utils');
2727const { BuiltinModule } = require ( 'internal/bootstrap/loaders' ) ;
2828const {
2929 realpathSync,
30- statSync,
31- Stats,
3230} = require ( 'fs' ) ;
3331const { getOptionValue } = require ( 'internal/options' ) ;
3432// Do not eagerly grab .manifest, it may be in TDZ
@@ -59,6 +57,7 @@ const {
5957const { Module : CJSModule } = require ( 'internal/modules/cjs/loader' ) ;
6058const { getPackageConfig, getPackageScopeConfig } = require ( 'internal/modules/esm/package_config' ) ;
6159const { getConditionsSet } = require ( 'internal/modules/esm/utils' ) ;
60+ const { internalModuleStat } = internalBinding ( 'fs' ) ;
6261
6362/**
6463 * @typedef {import('internal/modules/esm/package_config.js').PackageConfig } PackageConfig
@@ -135,19 +134,12 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
135134
136135const realpathCache = new SafeMap ( ) ;
137136
138- /**
139- * @param {string | URL } path
140- * @returns {import('fs').Stats }
141- */
142- const tryStatSync =
143- ( path ) => statSync ( path , { throwIfNoEntry : false } ) ?? new Stats ( ) ;
144-
145137/**
146138 * @param {string | URL } url
147139 * @returns {boolean }
148140 */
149141function fileExists ( url ) {
150- return statSync ( url , { throwIfNoEntry : false } ) ?. isFile ( ) ?? false ;
142+ return internalModuleStat ( url . pathname ?? url ) === 0 ;
151143}
152144
153145/**
@@ -218,13 +210,16 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
218210
219211 const path = fileURLToPath ( resolved ) ;
220212
221- const stats = tryStatSync ( StringPrototypeEndsWith ( path , '/' ) ?
213+ const stats = internalModuleStat ( StringPrototypeEndsWith ( path , '/' ) ?
222214 StringPrototypeSlice ( path , - 1 ) : path ) ;
223- if ( stats . isDirectory ( ) ) {
215+
216+ // Check for stats.isDirectory()
217+ if ( stats === 1 ) {
224218 const err = new ERR_UNSUPPORTED_DIR_IMPORT ( path , fileURLToPath ( base ) ) ;
225219 err . url = String ( resolved ) ;
226220 throw err ;
227- } else if ( ! stats . isFile ( ) ) {
221+ } else if ( stats !== 0 ) {
222+ // Check for !stats.isFile()
228223 if ( process . env . WATCH_REPORT_DEPENDENCIES && process . send ) {
229224 process . send ( { 'watch:require' : [ path || resolved . pathname ] } ) ;
230225 }
@@ -760,9 +755,11 @@ function packageResolve(specifier, base, conditions) {
760755 let packageJSONPath = fileURLToPath ( packageJSONUrl ) ;
761756 let lastPath ;
762757 do {
763- const stat = tryStatSync ( StringPrototypeSlice ( packageJSONPath , 0 ,
764- packageJSONPath . length - 13 ) ) ;
765- if ( ! stat . isDirectory ( ) ) {
758+ const stat = internalModuleStat ( StringPrototypeSlice ( packageJSONPath , 0 ,
759+ packageJSONPath . length - 13 ) ) ;
760+
761+ // Check for !stat.isDirectory
762+ if ( stat !== 1 ) {
766763 lastPath = packageJSONPath ;
767764 packageJSONUrl = new URL ( ( isScoped ?
768765 '../../../../node_modules/' : '../../../node_modules/' ) +
0 commit comments