@@ -427,7 +427,7 @@ ObjectDefineProperty(Module, '_readPackage', {
427
427
* @param {string } originalPath The specifier passed to `require`
428
428
*/
429
429
function tryPackage ( requestPath , exts , isMain , originalPath ) {
430
- const pkg = _readPackage ( requestPath ) . main ;
430
+ const { main : pkg , pjsonPath } = _readPackage ( requestPath ) ;
431
431
432
432
if ( ! pkg ) {
433
433
return tryExtensions ( path . resolve ( requestPath , 'index' ) , exts , isMain ) ;
@@ -446,14 +446,13 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
446
446
'Please verify that the package.json has a valid "main" entry' ,
447
447
) ;
448
448
err . code = 'MODULE_NOT_FOUND' ;
449
- err . path = path . resolve ( requestPath , 'package.json' ) ;
449
+ err . path = pjsonPath ;
450
450
err . requestPath = originalPath ;
451
451
// TODO(BridgeAR): Add the requireStack as well.
452
452
throw err ;
453
453
} else {
454
- const jsonPath = path . resolve ( requestPath , 'package.json' ) ;
455
454
process . emitWarning (
456
- `Invalid 'main' field in '${ jsonPath } ' of '${ pkg } '. ` +
455
+ `Invalid 'main' field in '${ pjsonPath } ' of '${ pkg } '. ` +
457
456
'Please either fix that or report it to the module author' ,
458
457
'DeprecationWarning' ,
459
458
'DEP0128' ,
@@ -539,28 +538,28 @@ function trySelfParentPath(parent) {
539
538
function trySelf ( parentPath , request ) {
540
539
if ( ! parentPath ) { return false ; }
541
540
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 ) {
544
543
return false ;
545
544
}
546
545
547
546
let expansion ;
548
- if ( request === pkg . name ) {
547
+ if ( request === pkg . data . name ) {
549
548
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 ) ;
552
551
} else {
553
552
return false ;
554
553
}
555
554
556
555
try {
557
556
const { packageExportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
558
557
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 ) ;
561
560
} catch ( e ) {
562
561
if ( e . code === 'ERR_MODULE_NOT_FOUND' ) {
563
- throw createEsmNotFoundErr ( request , pkgPath + '/package.json' ) ;
562
+ throw createEsmNotFoundErr ( request , pkg . path + '/package.json' ) ;
564
563
}
565
564
throw e ;
566
565
}
@@ -1099,7 +1098,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
1099
1098
1100
1099
if ( request [ 0 ] === '#' && ( parent ?. filename || parent ?. id === '<repl>' ) ) {
1101
1100
const parentPath = parent ?. filename ?? process . cwd ( ) + path . sep ;
1102
- const pkg = packageJsonReader . readPackageScope ( parentPath ) || { __proto__ : null } ;
1101
+ const pkg = packageJsonReader . getNearestParentPackageJSON ( parentPath ) || { __proto__ : null } ;
1103
1102
if ( pkg . data ?. imports != null ) {
1104
1103
try {
1105
1104
const { packageImportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
@@ -1397,9 +1396,9 @@ Module._extensions['.js'] = function(module, filename) {
1397
1396
content = fs . readFileSync ( filename , 'utf8' ) ;
1398
1397
}
1399
1398
if ( StringPrototypeEndsWith ( filename , '.js' ) ) {
1400
- const pkg = packageJsonReader . readPackageScope ( filename ) || { __proto__ : null } ;
1399
+ const pkg = packageJsonReader . getNearestParentPackageJSON ( filename ) ;
1401
1400
// Function require shouldn't be used in ES modules.
1402
- if ( pkg . data ? .type === 'module' ) {
1401
+ if ( pkg ? .data . type === 'module' ) {
1403
1402
// This is an error path because `require` of a `.js` file in a `"type": "module"` scope is not allowed.
1404
1403
const parent = moduleParentCache . get ( module ) ;
1405
1404
const parentPath = parent ?. filename ;
0 commit comments