@@ -82,7 +82,6 @@ const {
82
82
pendingDeprecate,
83
83
emitExperimentalWarning,
84
84
kEmptyObject,
85
- filterOwnProperties,
86
85
setOwnProperty,
87
86
getLazy,
88
87
} = require ( 'internal/util' ) ;
@@ -355,36 +354,12 @@ function initializeCJS() {
355
354
// -> a.<ext>
356
355
// -> a/index.<ext>
357
356
358
- const packageJsonCache = new SafeMap ( ) ;
359
-
357
+ /**
358
+ * @param {string } requestPath
359
+ * @return {PackageConfig }
360
+ */
360
361
function 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' ) ) ;
388
363
}
389
364
390
365
let _readPackage = readPackage ;
@@ -414,7 +389,7 @@ function readPackageScope(checkPath) {
414
389
if ( StringPrototypeEndsWith ( checkPath , sep + 'node_modules' ) )
415
390
return false ;
416
391
const pjson = _readPackage ( checkPath + sep ) ;
417
- if ( pjson ) return {
392
+ if ( pjson . exists ) return {
418
393
data : pjson ,
419
394
path : checkPath ,
420
395
} ;
@@ -423,7 +398,7 @@ function readPackageScope(checkPath) {
423
398
}
424
399
425
400
function tryPackage ( requestPath , exts , isMain , originalPath ) {
426
- const pkg = _readPackage ( requestPath ) ? .main ;
401
+ const pkg = _readPackage ( requestPath ) . main ;
427
402
428
403
if ( ! pkg ) {
429
404
return tryExtensions ( path . resolve ( requestPath , 'index' ) , exts , isMain ) ;
@@ -528,9 +503,10 @@ function trySelfParentPath(parent) {
528
503
function trySelf ( parentPath , request ) {
529
504
if ( ! parentPath ) return false ;
530
505
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
+ }
534
510
535
511
let expansion ;
536
512
if ( request === pkg . name ) {
@@ -565,7 +541,7 @@ function resolveExports(nmPath, request) {
565
541
return ;
566
542
const pkgPath = path . resolve ( nmPath , name ) ;
567
543
const pkg = _readPackage ( pkgPath ) ;
568
- if ( pkg ? .exports != null ) {
544
+ if ( pkg . exists && pkg . exports != null ) {
569
545
try {
570
546
const { packageExportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
571
547
return finalizeEsmResolution ( packageExportsResolve (
@@ -1028,7 +1004,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
1028
1004
1029
1005
if ( request [ 0 ] === '#' && ( parent ?. filename || parent ?. id === '<repl>' ) ) {
1030
1006
const parentPath = parent ?. filename ?? process . cwd ( ) + path . sep ;
1031
- const pkg = readPackageScope ( parentPath ) || { } ;
1007
+ const pkg = readPackageScope ( parentPath ) || { __proto__ : null } ;
1032
1008
if ( pkg . data ?. imports != null ) {
1033
1009
try {
1034
1010
const { packageImportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
@@ -1274,9 +1250,9 @@ Module._extensions['.js'] = function(module, filename) {
1274
1250
content = fs . readFileSync ( filename , 'utf8' ) ;
1275
1251
}
1276
1252
if ( StringPrototypeEndsWith ( filename , '.js' ) ) {
1277
- const pkg = readPackageScope ( filename ) ;
1253
+ const pkg = readPackageScope ( filename ) || { __proto__ : null } ;
1278
1254
// Function require shouldn't be used in ES modules.
1279
- if ( pkg ? .data ?. type === 'module' ) {
1255
+ if ( pkg . data ?. type === 'module' ) {
1280
1256
const parent = moduleParentCache . get ( module ) ;
1281
1257
const parentPath = parent ?. filename ;
1282
1258
const packageJsonPath = path . resolve ( pkg . path , 'package.json' ) ;
0 commit comments