@@ -470,7 +470,7 @@ ObjectDefineProperty(Module, '_readPackage', {
470470 * @param {string } originalPath The specifier passed to `require`
471471 */
472472function tryPackage ( requestPath , exts , isMain , originalPath ) {
473- const pkg = _readPackage ( requestPath ) . main ;
473+ const { main : pkg , pjsonPath } = _readPackage ( requestPath ) ;
474474
475475 if ( ! pkg ) {
476476 return tryExtensions ( path . resolve ( requestPath , 'index' ) , exts , isMain ) ;
@@ -489,14 +489,13 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
489489 'Please verify that the package.json has a valid "main" entry' ,
490490 ) ;
491491 err . code = 'MODULE_NOT_FOUND' ;
492- err . path = path . resolve ( requestPath , 'package.json' ) ;
492+ err . path = pjsonPath ;
493493 err . requestPath = originalPath ;
494494 // TODO(BridgeAR): Add the requireStack as well.
495495 throw err ;
496496 } else {
497- const jsonPath = path . resolve ( requestPath , 'package.json' ) ;
498497 process . emitWarning (
499- `Invalid 'main' field in '${ jsonPath } ' of '${ pkg } '. ` +
498+ `Invalid 'main' field in '${ pjsonPath } ' of '${ pkg } '. ` +
500499 'Please either fix that or report it to the module author' ,
501500 'DeprecationWarning' ,
502501 'DEP0128' ,
@@ -582,28 +581,28 @@ function trySelfParentPath(parent) {
582581function trySelf ( parentPath , request ) {
583582 if ( ! parentPath ) { return false ; }
584583
585- const { data : pkg , path : pkgPath } = packageJsonReader . readPackageScope ( parentPath ) ;
586- if ( ! pkg || pkg . exports == null || pkg . name === undefined ) {
584+ const pkg = packageJsonReader . getNearestParentPackageJSON ( parentPath ) ;
585+ if ( pkg ?. data . exports === undefined || pkg . data . name === undefined ) {
587586 return false ;
588587 }
589588
590589 let expansion ;
591- if ( request === pkg . name ) {
590+ if ( request === pkg . data . name ) {
592591 expansion = '.' ;
593- } else if ( StringPrototypeStartsWith ( request , `${ pkg . name } /` ) ) {
594- expansion = '.' + StringPrototypeSlice ( request , pkg . name . length ) ;
592+ } else if ( StringPrototypeStartsWith ( request , `${ pkg . data . name } /` ) ) {
593+ expansion = '.' + StringPrototypeSlice ( request , pkg . data . name . length ) ;
595594 } else {
596595 return false ;
597596 }
598597
599598 try {
600599 const { packageExportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
601600 return finalizeEsmResolution ( packageExportsResolve (
602- pathToFileURL ( pkgPath + '/package.json' ) , expansion , pkg ,
603- pathToFileURL ( parentPath ) , getCjsConditions ( ) ) , parentPath , pkgPath ) ;
601+ pathToFileURL ( pkg . path + '/package.json' ) , expansion , pkg . data ,
602+ pathToFileURL ( parentPath ) , getCjsConditions ( ) ) , parentPath , pkg . path ) ;
604603 } catch ( e ) {
605604 if ( e . code === 'ERR_MODULE_NOT_FOUND' ) {
606- throw createEsmNotFoundErr ( request , pkgPath + '/package.json' ) ;
605+ throw createEsmNotFoundErr ( request , pkg . path + '/package.json' ) ;
607606 }
608607 throw e ;
609608 }
@@ -1180,7 +1179,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
11801179
11811180 if ( request [ 0 ] === '#' && ( parent ?. filename || parent ?. id === '<repl>' ) ) {
11821181 const parentPath = parent ?. filename ?? process . cwd ( ) + path . sep ;
1183- const pkg = packageJsonReader . readPackageScope ( parentPath ) || { __proto__ : null } ;
1182+ const pkg = packageJsonReader . getNearestParentPackageJSON ( parentPath ) || { __proto__ : null } ;
11841183 if ( pkg . data ?. imports != null ) {
11851184 try {
11861185 const { packageImportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
@@ -1504,9 +1503,9 @@ Module._extensions['.js'] = function(module, filename) {
15041503 const content = getMaybeCachedSource ( module , filename ) ;
15051504
15061505 if ( StringPrototypeEndsWith ( filename , '.js' ) ) {
1507- const pkg = packageJsonReader . readPackageScope ( filename ) || { __proto__ : null } ;
1506+ const pkg = packageJsonReader . getNearestParentPackageJSON ( filename ) ;
15081507 // Function require shouldn't be used in ES modules.
1509- if ( pkg . data ? .type === 'module' ) {
1508+ if ( pkg ? .data . type === 'module' ) {
15101509 if ( getOptionValue ( '--experimental-require-module' ) ) {
15111510 module . _compile ( content , filename , true ) ;
15121511 return ;
0 commit comments