@@ -56,7 +56,6 @@ const {
5656 StringPrototypeCharAt,
5757 StringPrototypeCharCodeAt,
5858 StringPrototypeEndsWith,
59- StringPrototypeLastIndexOf,
6059 StringPrototypeIndexOf,
6160 StringPrototypeRepeat,
6261 StringPrototypeSlice,
@@ -69,7 +68,7 @@ const cjsParseCache = new SafeWeakMap();
6968
7069// Set first due to cycle with ESM loader functions.
7170module . exports = {
72- wrapSafe, Module, toRealPath , readPackageScope , cjsParseCache,
71+ wrapSafe, Module, cjsParseCache,
7372 get hasLoadedAnyUserCJSModule ( ) { return hasLoadedAnyUserCJSModule ; } ,
7473 initializeCJS,
7574} ;
@@ -89,9 +88,7 @@ const {
8988const { internalCompileFunction } = require ( 'internal/vm' ) ;
9089const assert = require ( 'internal/assert' ) ;
9190const fs = require ( 'fs' ) ;
92- const internalFS = require ( 'internal/fs/utils' ) ;
9391const path = require ( 'path' ) ;
94- const { sep } = path ;
9592const { internalModuleStat } = internalBinding ( 'fs' ) ;
9693const { safeGetenv } = internalBinding ( 'credentials' ) ;
9794const {
@@ -107,6 +104,7 @@ const {
107104 makeRequireFunction,
108105 normalizeReferrerURL,
109106 stripBOM,
107+ toRealPath,
110108} = require ( 'internal/modules/helpers' ) ;
111109const packageJsonReader = require ( 'internal/modules/package_json_reader' ) ;
112110const { getOptionValue, getEmbedderOptions } = require ( 'internal/options' ) ;
@@ -402,15 +400,7 @@ function initializeCJS() {
402400// -> a.<ext>
403401// -> a/index.<ext>
404402
405- /**
406- * @param {string } requestPath
407- * @return {PackageConfig }
408- */
409- function readPackage ( requestPath ) {
410- return packageJsonReader . read ( path . resolve ( requestPath , 'package.json' ) ) ;
411- }
412-
413- let _readPackage = readPackage ;
403+ let _readPackage = packageJsonReader . readPackage ;
414404ObjectDefineProperty ( Module , '_readPackage' , {
415405 __proto__ : null ,
416406 get ( ) { return _readPackage ; } ,
@@ -422,31 +412,6 @@ ObjectDefineProperty(Module, '_readPackage', {
422412 configurable : true ,
423413} ) ;
424414
425- /**
426- * Get the nearest parent package.json file from a given path.
427- * Return the package.json data and the path to the package.json file, or false.
428- * @param {string } checkPath The path to start searching from.
429- */
430- function readPackageScope ( checkPath ) {
431- const rootSeparatorIndex = StringPrototypeIndexOf ( checkPath , sep ) ;
432- let separatorIndex ;
433- do {
434- separatorIndex = StringPrototypeLastIndexOf ( checkPath , sep ) ;
435- checkPath = StringPrototypeSlice ( checkPath , 0 , separatorIndex ) ;
436- if ( StringPrototypeEndsWith ( checkPath , sep + 'node_modules' ) ) {
437- return false ;
438- }
439- const pjson = _readPackage ( checkPath + sep ) ;
440- if ( pjson . exists ) {
441- return {
442- data : pjson ,
443- path : checkPath ,
444- } ;
445- }
446- } while ( separatorIndex > rootSeparatorIndex ) ;
447- return false ;
448- }
449-
450415/**
451416 * Try to load a specifier as a package.
452417 * @param {string } requestPath The path to what we are trying to load
@@ -491,14 +456,6 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
491456 return actual ;
492457}
493458
494- /**
495- * Cache for storing resolved real paths of modules.
496- * In order to minimize unnecessary lstat() calls, this cache is a list of known-real paths.
497- * Set to an empty Map to reset.
498- * @type {Map<string, string> }
499- */
500- const realpathCache = new SafeMap ( ) ;
501-
502459/**
503460 * Check if the file exists and is not a directory if using `--preserve-symlinks` and `isMain` is false, keep symlinks
504461 * intact, otherwise resolve to the absolute realpath.
@@ -514,17 +471,6 @@ function tryFile(requestPath, isMain) {
514471 return toRealPath ( requestPath ) ;
515472}
516473
517-
518- /**
519- * Resolves the path of a given `require` specifier, following symlinks.
520- * @param {string } requestPath The `require` specifier
521- */
522- function toRealPath ( requestPath ) {
523- return fs . realpathSync ( requestPath , {
524- [ internalFS . realpathCacheKey ] : realpathCache ,
525- } ) ;
526- }
527-
528474/**
529475 * Given a path, check if the file exists with any of the set extensions.
530476 * @param {string } basePath The path and filename without extension
@@ -586,7 +532,7 @@ function trySelfParentPath(parent) {
586532function trySelf ( parentPath , request ) {
587533 if ( ! parentPath ) { return false ; }
588534
589- const { data : pkg , path : pkgPath } = readPackageScope ( parentPath ) ;
535+ const { data : pkg , path : pkgPath } = packageJsonReader . readPackageScope ( parentPath ) ;
590536 if ( ! pkg || pkg . exports == null || pkg . name === undefined ) {
591537 return false ;
592538 }
@@ -1143,7 +1089,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
11431089
11441090 if ( request [ 0 ] === '#' && ( parent ?. filename || parent ?. id === '<repl>' ) ) {
11451091 const parentPath = parent ?. filename ?? process . cwd ( ) + path . sep ;
1146- const pkg = readPackageScope ( parentPath ) || { __proto__ : null } ;
1092+ const pkg = packageJsonReader . readPackageScope ( parentPath ) || { __proto__ : null } ;
11471093 if ( pkg . data ?. imports != null ) {
11481094 try {
11491095 const { packageImportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
@@ -1431,7 +1377,7 @@ Module._extensions['.js'] = function(module, filename) {
14311377 content = fs . readFileSync ( filename , 'utf8' ) ;
14321378 }
14331379 if ( StringPrototypeEndsWith ( filename , '.js' ) ) {
1434- const pkg = readPackageScope ( filename ) || { __proto__ : null } ;
1380+ const pkg = packageJsonReader . readPackageScope ( filename ) || { __proto__ : null } ;
14351381 // Function require shouldn't be used in ES modules.
14361382 if ( pkg . data ?. type === 'module' ) {
14371383 const parent = moduleParentCache . get ( module ) ;
0 commit comments