@@ -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
432432if ( ! pkg ) {
433433return 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) ;
448448err . code = 'MODULE_NOT_FOUND' ;
449- err . path = path . resolve ( requestPath , 'package.json' ) ;
449+ err . path = pjsonPath ;
450450err . requestPath = originalPath ;
451451// TODO(BridgeAR): Add the requireStack as well.
452452throw err ;
453453} else {
454- const jsonPath = path . resolve ( requestPath , 'package.json' ) ;
455454process . 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 ) {
540539if ( ! 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 ) {
544543return false ;
545544}
546545
547546let expansion ;
548- if ( request === pkg . name ) {
547+ if ( request === pkg . data . name ) {
549548expansion = '.' ;
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 {
553552return false ;
554553}
555554
556555try {
557556const { packageExportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
558557return 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 ) {
562561if ( e . code === 'ERR_MODULE_NOT_FOUND' ) {
563- throw createEsmNotFoundErr ( request , pkgPath + '/package.json' ) ;
562+ throw createEsmNotFoundErr ( request , pkg . path + '/package.json' ) ;
564563}
565564throw e ;
566565}
@@ -1099,7 +1098,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
10991098
11001099if ( request [ 0 ] === '#' && ( parent ?. filename || parent ?. id === '<repl>' ) ) {
11011100const parentPath = parent ?. filename ?? process . cwd ( ) + path . sep ;
1102- const pkg = packageJsonReader . readPackageScope ( parentPath ) || { __proto__ : null } ;
1101+ const pkg = packageJsonReader . getNearestParentPackageJSON ( parentPath ) || { __proto__ : null } ;
11031102if ( pkg . data ?. imports != null ) {
11041103try {
11051104const { packageImportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
@@ -1397,9 +1396,9 @@ Module._extensions['.js'] = function(module, filename) {
13971396content = fs . readFileSync ( filename , 'utf8' ) ;
13981397}
13991398if ( 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.
14041403const parent = moduleParentCache . get ( module ) ;
14051404const parentPath = parent ?. filename ;
0 commit comments