@@ -97,6 +97,11 @@ const { sep } = path;
9797const { internalModuleStat } = internalBinding ( 'fs' ) ;
9898const packageJsonReader = require ( 'internal/modules/package_json_reader' ) ;
9999const { safeGetenv } = internalBinding ( 'credentials' ) ;
100+ const {
101+ privateSymbols : {
102+ require_private_symbol,
103+ } ,
104+ } = internalBinding ( 'util' ) ;
100105const {
101106 cjsConditions,
102107 hasEsmSyntax,
@@ -158,6 +163,20 @@ let requireDepth = 0;
158163let statCache = null ;
159164let isPreloading = false ;
160165
166+ function internalRequire ( module , id ) {
167+ validateString ( id , 'id' ) ;
168+ if ( id === '' ) {
169+ throw new ERR_INVALID_ARG_VALUE ( 'id' , id ,
170+ 'must be a non-empty string' ) ;
171+ }
172+ requireDepth ++ ;
173+ try {
174+ return Module . _load ( id , module , /* isMain */ false ) ;
175+ } finally {
176+ requireDepth -- ;
177+ }
178+ }
179+
161180function stat ( filename ) {
162181 filename = path . toNamespacedPath ( filename ) ;
163182 if ( statCache !== null ) {
@@ -212,6 +231,15 @@ function Module(id = '', parent) {
212231 this . filename = null ;
213232 this . loaded = false ;
214233 this . children = [ ] ;
234+ let redirects ;
235+ if ( policy ?. manifest ) {
236+ const moduleURL = pathToFileURL ( id ) ;
237+ redirects = policy . manifest . getDependencyMapper ( moduleURL ) ;
238+ }
239+ setOwnProperty ( this , 'require' , makeRequireFunction ( this , redirects ) ) ;
240+ // Loads a module at the given file path. Returns that module's
241+ // `exports` property.
242+ this [ require_private_symbol ] = internalRequire ;
215243}
216244
217245const builtinModules = [ ] ;
@@ -915,6 +943,7 @@ Module._load = function(request, parent, isMain) {
915943
916944 if ( isMain ) {
917945 process . mainModule = module ;
946+ setOwnProperty ( module . require , 'main' , process . mainModule ) ;
918947 module . id = '.' ;
919948 }
920949
@@ -1099,24 +1128,6 @@ Module.prototype.load = function(filename) {
10991128 esmLoader . cjsCache . set ( this , exports ) ;
11001129} ;
11011130
1102-
1103- // Loads a module at the given file path. Returns that module's
1104- // `exports` property.
1105- Module . prototype . require = function ( id ) {
1106- validateString ( id , 'id' ) ;
1107- if ( id === '' ) {
1108- throw new ERR_INVALID_ARG_VALUE ( 'id' , id ,
1109- 'must be a non-empty string' ) ;
1110- }
1111- requireDepth ++ ;
1112- try {
1113- return Module . _load ( id , this , /* isMain */ false ) ;
1114- } finally {
1115- requireDepth -- ;
1116- }
1117- } ;
1118-
1119-
11201131// Resolved path to process.argv[1] will be lazily placed here
11211132// (needed for setting breakpoint when called with --inspect-brk)
11221133let resolvedArgv ;
@@ -1180,10 +1191,9 @@ function wrapSafe(filename, content, cjsModuleInstance) {
11801191// Returns exception, if any.
11811192Module . prototype . _compile = function ( content , filename ) {
11821193 let moduleURL ;
1183- let redirects ;
11841194 if ( policy ?. manifest ) {
11851195 moduleURL = pathToFileURL ( filename ) ;
1186- redirects = policy . manifest . getDependencyMapper ( moduleURL ) ;
1196+ policy . manifest . getDependencyMapper ( moduleURL ) ;
11871197 policy . manifest . assertIntegrity ( moduleURL , content ) ;
11881198 }
11891199
@@ -1213,18 +1223,17 @@ Module.prototype._compile = function(content, filename) {
12131223 }
12141224 }
12151225 const dirname = path . dirname ( filename ) ;
1216- const require = makeRequireFunction ( this , redirects ) ;
12171226 let result ;
12181227 const exports = this . exports ;
12191228 const thisValue = exports ;
12201229 const module = this ;
12211230 if ( requireDepth === 0 ) statCache = new SafeMap ( ) ;
12221231 if ( inspectorWrapper ) {
12231232 result = inspectorWrapper ( compiledWrapper , thisValue , exports ,
1224- require , module , filename , dirname ) ;
1233+ module . require , module , filename , dirname ) ;
12251234 } else {
12261235 result = ReflectApply ( compiledWrapper , thisValue ,
1227- [ exports , require , module , filename , dirname ] ) ;
1236+ [ exports , module . require , module , filename , dirname ] ) ;
12281237 }
12291238 hasLoadedAnyUserCJSModule = true ;
12301239 if ( requireDepth === 0 ) statCache = null ;
@@ -1400,7 +1409,7 @@ Module._preloadModules = function(requests) {
14001409 }
14011410 }
14021411 for ( let n = 0 ; n < requests . length ; n ++ )
1403- parent . require ( requests [ n ] ) ;
1412+ internalRequire ( parent , requests [ n ] ) ;
14041413 isPreloading = false ;
14051414} ;
14061415
0 commit comments