@@ -25,24 +25,20 @@ const {
2525} = primordials ;
2626const internalFS = require ( 'internal/fs/utils' ) ;
2727const { BuiltinModule } = require ( 'internal/bootstrap/realm' ) ;
28- const {
29- realpathSync,
30- statSync,
31- Stats,
32- } = require ( 'fs' ) ;
28+ const { realpathSync } = require ( 'fs' ) ;
3329const { getOptionValue } = require ( 'internal/options' ) ;
3430const pendingDeprecation = getOptionValue ( '--pending-deprecation' ) ;
3531// Do not eagerly grab .manifest, it may be in TDZ
3632const policy = getOptionValue ( '--experimental-policy' ) ?
3733 require ( 'internal/process/policy' ) :
3834 null ;
39- const { sep, relative, resolve } = require ( 'path' ) ;
35+ const { sep, relative, resolve, toNamespacedPath } = require ( 'path' ) ;
4036const preserveSymlinks = getOptionValue ( '--preserve-symlinks' ) ;
4137const preserveSymlinksMain = getOptionValue ( '--preserve-symlinks-main' ) ;
4238const experimentalNetworkImports =
4339 getOptionValue ( '--experimental-network-imports' ) ;
4440const typeFlag = getOptionValue ( '--input-type' ) ;
45- const { URL , pathToFileURL, fileURLToPath } = require ( 'internal/url' ) ;
41+ const { URL , pathToFileURL, fileURLToPath, toPathIfFileURL } = require ( 'internal/url' ) ;
4642const {
4743 ERR_INPUT_TYPE_NOT_ALLOWED ,
4844 ERR_INVALID_MODULE_SPECIFIER ,
@@ -60,6 +56,7 @@ const { Module: CJSModule } = require('internal/modules/cjs/loader');
6056const packageJsonReader = require ( 'internal/modules/package_json_reader' ) ;
6157const { getPackageConfig, getPackageScopeConfig } = require ( 'internal/modules/esm/package_config' ) ;
6258const { getConditionsSet } = require ( 'internal/modules/esm/utils' ) ;
59+ const { internalModuleStat } = internalBinding ( 'fs' ) ;
6360
6461/**
6562 * @typedef {import('internal/modules/esm/package_config.js').PackageConfig } PackageConfig
@@ -138,19 +135,12 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
138135
139136const realpathCache = new SafeMap ( ) ;
140137
141- /**
142- * @param {string | URL } path
143- * @returns {import('fs').Stats }
144- */
145- const tryStatSync =
146- ( path ) => statSync ( path , { throwIfNoEntry : false } ) ?? new Stats ( ) ;
147-
148138/**
149139 * @param {string | URL } url
150140 * @returns {boolean }
151141 */
152142function fileExists ( url ) {
153- return statSync ( url , { throwIfNoEntry : false } ) ?. isFile ( ) ?? false ;
143+ return internalModuleStat ( toNamespacedPath ( toPathIfFileURL ( url ) ) ) === 0 ;
154144}
155145
156146/**
@@ -285,13 +275,16 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
285275 path = fileURLToPath ( resolved ) ;
286276 }
287277
288- const stats = tryStatSync ( StringPrototypeEndsWith ( path , '/' ) ?
289- StringPrototypeSlice ( path , - 1 ) : path ) ;
290- if ( stats . isDirectory ( ) ) {
278+ const stats = internalModuleStat ( toNamespacedPath ( StringPrototypeEndsWith ( path , '/' ) ?
279+ StringPrototypeSlice ( path , - 1 ) : path ) ) ;
280+
281+ // Check for stats.isDirectory()
282+ if ( stats === 1 ) {
291283 const err = new ERR_UNSUPPORTED_DIR_IMPORT ( path , fileURLToPath ( base ) ) ;
292284 err . url = String ( resolved ) ;
293285 throw err ;
294- } else if ( ! stats . isFile ( ) ) {
286+ } else if ( stats !== 0 ) {
287+ // Check for !stats.isFile()
295288 if ( process . env . WATCH_REPORT_DEPENDENCIES && process . send ) {
296289 process . send ( { 'watch:require' : [ path || resolved . pathname ] } ) ;
297290 }
@@ -826,9 +819,10 @@ function packageResolve(specifier, base, conditions) {
826819 let packageJSONPath = fileURLToPath ( packageJSONUrl ) ;
827820 let lastPath ;
828821 do {
829- const stat = tryStatSync ( StringPrototypeSlice ( packageJSONPath , 0 ,
830- packageJSONPath . length - 13 ) ) ;
831- if ( ! stat . isDirectory ( ) ) {
822+ const stat = internalModuleStat ( toNamespacedPath ( StringPrototypeSlice ( packageJSONPath , 0 ,
823+ packageJSONPath . length - 13 ) ) ) ;
824+ // Check for !stat.isDirectory()
825+ if ( stat !== 1 ) {
832826 lastPath = packageJSONPath ;
833827 packageJSONUrl = new URL ( ( isScoped ?
834828 '../../../../node_modules/' : '../../../node_modules/' ) +
0 commit comments