@@ -34,7 +34,7 @@ const { getOptionValue } = require('internal/options');
3434const policy = getOptionValue ( '--experimental-policy' ) ?
3535 require ( 'internal/process/policy' ) :
3636 null ;
37- const { sep, relative } = require ( 'path' ) ;
37+ const { sep, relative, resolve } = require ( 'path' ) ;
3838const preserveSymlinks = getOptionValue ( '--preserve-symlinks' ) ;
3939const preserveSymlinksMain = getOptionValue ( '--preserve-symlinks-main' ) ;
4040const typeFlag = getOptionValue ( '--input-type' ) ;
@@ -204,16 +204,18 @@ function getPackageScopeConfig(resolved) {
204204 return packageConfig ;
205205}
206206
207- /*
207+ /**
208208 * Legacy CommonJS main resolution:
209209 * 1. let M = pkg_url + (json main field)
210210 * 2. TRY(M, M.js, M.json, M.node)
211211 * 3. TRY(M/index.js, M/index.json, M/index.node)
212212 * 4. TRY(pkg_url/index.js, pkg_url/index.json, pkg_url/index.node)
213213 * 5. NOT_FOUND
214+ * @param {string | URL } url
215+ * @returns {boolean }
214216 */
215217function fileExists ( url ) {
216- return tryStatSync ( fileURLToPath ( url ) ) . isFile ( ) ;
218+ return statSync ( url , { throwIfNoEntry : false } ) ? .isFile ( ) ?? false ;
217219}
218220
219221function legacyMainResolve ( packageJSONUrl , packageConfig , base ) {
@@ -272,7 +274,19 @@ function resolveExtensions(search) {
272274 return undefined ;
273275}
274276
275- function resolveIndex ( search ) {
277+ function resolveDirectoryEntry ( search ) {
278+ const dirPath = fileURLToPath ( search ) ;
279+ const pkgJsonPath = resolve ( dirPath , 'package.json' ) ;
280+ if ( fileExists ( pkgJsonPath ) ) {
281+ const pkgJson = packageJsonReader . read ( pkgJsonPath ) ;
282+ if ( pkgJson . containsKeys ) {
283+ const { main } = JSONParse ( pkgJson . string ) ;
284+ if ( main != null ) {
285+ const mainUrl = pathToFileURL ( resolve ( dirPath , main ) ) ;
286+ return resolveExtensionsWithTryExactName ( mainUrl ) ;
287+ }
288+ }
289+ }
276290 return resolveExtensions ( new URL ( 'index' , search ) ) ;
277291}
278292
@@ -288,10 +302,10 @@ function finalizeResolution(resolved, base) {
288302 let file = resolveExtensionsWithTryExactName ( resolved ) ;
289303 if ( file !== undefined ) return file ;
290304 if ( ! StringPrototypeEndsWith ( path , '/' ) ) {
291- file = resolveIndex ( new URL ( `${ resolved } /` ) ) ;
305+ file = resolveDirectoryEntry ( new URL ( `${ resolved } /` ) ) ;
292306 if ( file !== undefined ) return file ;
293307 } else {
294- return resolveIndex ( resolved ) || resolved ;
308+ return resolveDirectoryEntry ( resolved ) || resolved ;
295309 }
296310 throw new ERR_MODULE_NOT_FOUND (
297311 resolved . pathname , fileURLToPath ( base ) , 'module' ) ;
0 commit comments