@@ -82,7 +82,6 @@ const {
8282 pendingDeprecate,
8383 emitExperimentalWarning,
8484 kEmptyObject,
85- filterOwnProperties,
8685 setOwnProperty,
8786 getLazy,
8887} = require ( 'internal/util' ) ;
@@ -355,36 +354,12 @@ function initializeCJS() {
355354// -> a.<ext>
356355// -> a/index.<ext>
357356
358- const packageJsonCache = new SafeMap ( ) ;
359-
357+ /**
358+ * @param {string } requestPath
359+ * @return {PackageConfig }
360+ */
360361function readPackage ( requestPath ) {
361- const jsonPath = path . resolve ( requestPath , 'package.json' ) ;
362-
363- const existing = packageJsonCache . get ( jsonPath ) ;
364- if ( existing !== undefined ) return existing ;
365-
366- const result = packageJsonReader . read ( jsonPath ) ;
367- const json = result . containsKeys === false ? '{}' : result . string ;
368- if ( json === undefined ) {
369- packageJsonCache . set ( jsonPath , false ) ;
370- return false ;
371- }
372-
373- try {
374- const filtered = filterOwnProperties ( JSONParse ( json ) , [
375- 'name' ,
376- 'main' ,
377- 'exports' ,
378- 'imports' ,
379- 'type' ,
380- ] ) ;
381- packageJsonCache . set ( jsonPath , filtered ) ;
382- return filtered ;
383- } catch ( e ) {
384- e . path = jsonPath ;
385- e . message = 'Error parsing ' + jsonPath + ': ' + e . message ;
386- throw e ;
387- }
362+ return packageJsonReader . read ( path . resolve ( requestPath , 'package.json' ) ) ;
388363}
389364
390365let _readPackage = readPackage ;
@@ -414,7 +389,7 @@ function readPackageScope(checkPath) {
414389 if ( StringPrototypeEndsWith ( checkPath , sep + 'node_modules' ) )
415390 return false ;
416391 const pjson = _readPackage ( checkPath + sep ) ;
417- if ( pjson ) return {
392+ if ( pjson . exists ) return {
418393 data : pjson ,
419394 path : checkPath ,
420395 } ;
@@ -423,7 +398,7 @@ function readPackageScope(checkPath) {
423398}
424399
425400function tryPackage ( requestPath , exts , isMain , originalPath ) {
426- const pkg = _readPackage ( requestPath ) ? .main ;
401+ const pkg = _readPackage ( requestPath ) . main ;
427402
428403 if ( ! pkg ) {
429404 return tryExtensions ( path . resolve ( requestPath , 'index' ) , exts , isMain ) ;
@@ -528,9 +503,10 @@ function trySelfParentPath(parent) {
528503function trySelf ( parentPath , request ) {
529504 if ( ! parentPath ) return false ;
530505
531- const { data : pkg , path : pkgPath } = readPackageScope ( parentPath ) || { } ;
532- if ( ! pkg || pkg . exports === undefined ) return false ;
533- if ( typeof pkg . name !== 'string' ) return false ;
506+ const { data : pkg , path : pkgPath } = readPackageScope ( parentPath ) ;
507+ if ( ! pkg || pkg . exports == null || pkg . name === undefined ) {
508+ return false ;
509+ }
534510
535511 let expansion ;
536512 if ( request === pkg . name ) {
@@ -565,7 +541,7 @@ function resolveExports(nmPath, request) {
565541 return ;
566542 const pkgPath = path . resolve ( nmPath , name ) ;
567543 const pkg = _readPackage ( pkgPath ) ;
568- if ( pkg ? .exports != null ) {
544+ if ( pkg . exists && pkg . exports != null ) {
569545 try {
570546 const { packageExportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
571547 return finalizeEsmResolution ( packageExportsResolve (
@@ -1028,7 +1004,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
10281004
10291005 if ( request [ 0 ] === '#' && ( parent ?. filename || parent ?. id === '<repl>' ) ) {
10301006 const parentPath = parent ?. filename ?? process . cwd ( ) + path . sep ;
1031- const pkg = readPackageScope ( parentPath ) || { } ;
1007+ const pkg = readPackageScope ( parentPath ) || { __proto__ : null } ;
10321008 if ( pkg . data ?. imports != null ) {
10331009 try {
10341010 const { packageImportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
@@ -1274,9 +1250,9 @@ Module._extensions['.js'] = function(module, filename) {
12741250 content = fs . readFileSync ( filename , 'utf8' ) ;
12751251 }
12761252 if ( StringPrototypeEndsWith ( filename , '.js' ) ) {
1277- const pkg = readPackageScope ( filename ) ;
1253+ const pkg = readPackageScope ( filename ) || { __proto__ : null } ;
12781254 // Function require shouldn't be used in ES modules.
1279- if ( pkg ? .data ?. type === 'module' ) {
1255+ if ( pkg . data ?. type === 'module' ) {
12801256 const parent = moduleParentCache . get ( module ) ;
12811257 const parentPath = parent ?. filename ;
12821258 const packageJsonPath = path . resolve ( pkg . path , 'package.json' ) ;
0 commit comments