@@ -427,7 +427,7 @@ ObjectDefineProperty(Module, '_readPackage', {
427427 * @param {string } originalPath The specifier passed to `require`
428428 */
429429function tryPackage ( requestPath , exts , isMain , originalPath ) {
430- const pkg = _readPackage ( requestPath ) . main ;
430+ const { main : pkg , pjsonPath } = _readPackage ( requestPath ) ;
431431
432432 if ( ! pkg ) {
433433 return tryExtensions ( path . resolve ( requestPath , 'index' ) , exts , isMain ) ;
@@ -446,14 +446,13 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
446446 'Please verify that the package.json has a valid "main" entry' ,
447447 ) ;
448448 err . code = 'MODULE_NOT_FOUND' ;
449- err . path = path . resolve ( requestPath , 'package.json' ) ;
449+ err . path = pjsonPath ;
450450 err . requestPath = originalPath ;
451451 // TODO(BridgeAR): Add the requireStack as well.
452452 throw err ;
453453 } else {
454- const jsonPath = path . resolve ( requestPath , 'package.json' ) ;
455454 process . emitWarning (
456- `Invalid 'main' field in '${ jsonPath } ' of '${ pkg } '. ` +
455+ `Invalid 'main' field in '${ pjsonPath } ' of '${ pkg } '. ` +
457456 'Please either fix that or report it to the module author' ,
458457 'DeprecationWarning' ,
459458 'DEP0128' ,
@@ -539,28 +538,28 @@ function trySelfParentPath(parent) {
539538function trySelf ( parentPath , request ) {
540539 if ( ! parentPath ) { return false ; }
541540
542- const { data : pkg , path : pkgPath } = packageJsonReader . readPackageScope ( parentPath ) ;
543- if ( ! pkg || pkg . exports == null || pkg . name === undefined ) {
541+ const pkg = packageJsonReader . getNearestParentPackageJSON ( parentPath ) ;
542+ if ( pkg ?. data . exports === undefined || pkg . data . name === undefined ) {
544543 return false ;
545544 }
546545
547546 let expansion ;
548- if ( request === pkg . name ) {
547+ if ( request === pkg . data . name ) {
549548 expansion = '.' ;
550- } else if ( StringPrototypeStartsWith ( request , `${ pkg . name } /` ) ) {
551- expansion = '.' + StringPrototypeSlice ( request , pkg . name . length ) ;
549+ } else if ( StringPrototypeStartsWith ( request , `${ pkg . data . name } /` ) ) {
550+ expansion = '.' + StringPrototypeSlice ( request , pkg . data . name . length ) ;
552551 } else {
553552 return false ;
554553 }
555554
556555 try {
557556 const { packageExportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
558557 return finalizeEsmResolution ( packageExportsResolve (
559- pathToFileURL ( pkgPath + '/package.json' ) , expansion , pkg ,
560- pathToFileURL ( parentPath ) , getCjsConditions ( ) ) , parentPath , pkgPath ) ;
558+ pathToFileURL ( pkg . path + '/package.json' ) , expansion , pkg . data ,
559+ pathToFileURL ( parentPath ) , getCjsConditions ( ) ) , parentPath , pkg . path ) ;
561560 } catch ( e ) {
562561 if ( e . code === 'ERR_MODULE_NOT_FOUND' ) {
563- throw createEsmNotFoundErr ( request , pkgPath + '/package.json' ) ;
562+ throw createEsmNotFoundErr ( request , pkg . path + '/package.json' ) ;
564563 }
565564 throw e ;
566565 }
@@ -1099,7 +1098,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
10991098
11001099 if ( request [ 0 ] === '#' && ( parent ?. filename || parent ?. id === '<repl>' ) ) {
11011100 const parentPath = parent ?. filename ?? process . cwd ( ) + path . sep ;
1102- const pkg = packageJsonReader . readPackageScope ( parentPath ) || { __proto__ : null } ;
1101+ const pkg = packageJsonReader . getNearestParentPackageJSON ( parentPath ) || { __proto__ : null } ;
11031102 if ( pkg . data ?. imports != null ) {
11041103 try {
11051104 const { packageImportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
@@ -1397,9 +1396,9 @@ Module._extensions['.js'] = function(module, filename) {
13971396 content = fs . readFileSync ( filename , 'utf8' ) ;
13981397 }
13991398 if ( StringPrototypeEndsWith ( filename , '.js' ) ) {
1400- const pkg = packageJsonReader . readPackageScope ( filename ) || { __proto__ : null } ;
1399+ const pkg = packageJsonReader . getNearestParentPackageJSON ( filename ) ;
14011400 // Function require shouldn't be used in ES modules.
1402- if ( pkg . data ? .type === 'module' ) {
1401+ if ( pkg ? .data . type === 'module' ) {
14031402 // This is an error path because `require` of a `.js` file in a `"type": "module"` scope is not allowed.
14041403 const parent = moduleParentCache . get ( module ) ;
14051404 const parentPath = parent ?. filename ;
0 commit comments