@@ -82,7 +82,6 @@ const {
8282 pendingDeprecate,
8383 emitExperimentalWarning,
8484 kEmptyObject,
85- filterOwnProperties,
8685 setOwnProperty,
8786 getLazy,
8887} = require ( 'internal/util' ) ;
@@ -353,36 +352,12 @@ function initializeCJS() {
353352// -> a.<ext>
354353// -> a/index.<ext>
355354
356- const packageJsonCache = new SafeMap ( ) ;
357-
355+ /**
356+ * @param {string } requestPath
357+ * @return {PackageConfig }
358+ */
358359function readPackage ( requestPath ) {
359- const jsonPath = path . resolve ( requestPath , 'package.json' ) ;
360-
361- const existing = packageJsonCache . get ( jsonPath ) ;
362- if ( existing !== undefined ) return existing ;
363-
364- const result = packageJsonReader . read ( jsonPath ) ;
365- const json = result . containsKeys === false ? '{}' : result . string ;
366- if ( json === undefined ) {
367- packageJsonCache . set ( jsonPath , false ) ;
368- return false ;
369- }
370-
371- try {
372- const filtered = filterOwnProperties ( JSONParse ( json ) , [
373- 'name' ,
374- 'main' ,
375- 'exports' ,
376- 'imports' ,
377- 'type' ,
378- ] ) ;
379- packageJsonCache . set ( jsonPath , filtered ) ;
380- return filtered ;
381- } catch ( e ) {
382- e . path = jsonPath ;
383- e . message = 'Error parsing ' + jsonPath + ': ' + e . message ;
384- throw e ;
385- }
360+ return packageJsonReader . read ( path . resolve ( requestPath , 'package.json' ) ) ;
386361}
387362
388363let _readPackage = readPackage ;
@@ -412,7 +387,7 @@ function readPackageScope(checkPath) {
412387 if ( StringPrototypeEndsWith ( checkPath , sep + 'node_modules' ) )
413388 return false ;
414389 const pjson = _readPackage ( checkPath + sep ) ;
415- if ( pjson ) return {
390+ if ( pjson . exists ) return {
416391 data : pjson ,
417392 path : checkPath ,
418393 } ;
@@ -421,7 +396,7 @@ function readPackageScope(checkPath) {
421396}
422397
423398function tryPackage ( requestPath , exts , isMain , originalPath ) {
424- const pkg = _readPackage ( requestPath ) ? .main ;
399+ const pkg = _readPackage ( requestPath ) . main ;
425400
426401 if ( ! pkg ) {
427402 return tryExtensions ( path . resolve ( requestPath , 'index' ) , exts , isMain ) ;
@@ -526,9 +501,10 @@ function trySelfParentPath(parent) {
526501function trySelf ( parentPath , request ) {
527502 if ( ! parentPath ) return false ;
528503
529- const { data : pkg , path : pkgPath } = readPackageScope ( parentPath ) || { } ;
530- if ( ! pkg || pkg . exports === undefined ) return false ;
531- if ( typeof pkg . name !== 'string' ) return false ;
504+ const { data : pkg , path : pkgPath } = readPackageScope ( parentPath ) ;
505+ if ( ! pkg || pkg . exports == null || pkg . name === undefined ) {
506+ return false ;
507+ }
532508
533509 let expansion ;
534510 if ( request === pkg . name ) {
@@ -563,7 +539,7 @@ function resolveExports(nmPath, request) {
563539 return ;
564540 const pkgPath = path . resolve ( nmPath , name ) ;
565541 const pkg = _readPackage ( pkgPath ) ;
566- if ( pkg ? .exports != null ) {
542+ if ( pkg . exists && pkg . exports != null ) {
567543 try {
568544 const { packageExportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
569545 return finalizeEsmResolution ( packageExportsResolve (
@@ -1026,7 +1002,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
10261002
10271003 if ( request [ 0 ] === '#' && ( parent ?. filename || parent ?. id === '<repl>' ) ) {
10281004 const parentPath = parent ?. filename ?? process . cwd ( ) + path . sep ;
1029- const pkg = readPackageScope ( parentPath ) || { } ;
1005+ const pkg = readPackageScope ( parentPath ) || { __proto__ : null } ;
10301006 if ( pkg . data ?. imports != null ) {
10311007 try {
10321008 const { packageImportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
@@ -1272,9 +1248,9 @@ Module._extensions['.js'] = function(module, filename) {
12721248 content = fs . readFileSync ( filename , 'utf8' ) ;
12731249 }
12741250 if ( StringPrototypeEndsWith ( filename , '.js' ) ) {
1275- const pkg = readPackageScope ( filename ) ;
1251+ const pkg = readPackageScope ( filename ) || { __proto__ : null } ;
12761252 // Function require shouldn't be used in ES modules.
1277- if ( pkg ? .data ?. type === 'module' ) {
1253+ if ( pkg . data ?. type === 'module' ) {
12781254 const parent = moduleParentCache . get ( module ) ;
12791255 const parentPath = parent ?. filename ;
12801256 const packageJsonPath = path . resolve ( pkg . path , 'package.json' ) ;
0 commit comments