@@ -95,6 +95,9 @@ const { sep } = path;
9595const { internalModuleStat } = internalBinding ( 'fs' ) ;
9696const packageJsonReader = require ( 'internal/modules/package_json_reader' ) ;
9797const { safeGetenv } = internalBinding ( 'credentials' ) ;
98+ const {
99+ require_private_symbol,
100+ } = internalBinding ( 'util' ) ;
98101const {
99102 cjsConditions,
100103 hasEsmSyntax,
@@ -155,6 +158,20 @@ let requireDepth = 0;
155158let statCache = null ;
156159let isPreloading = false ;
157160
161+ function internalRequire ( module , id ) {
162+ validateString ( id , 'id' ) ;
163+ if ( id === '' ) {
164+ throw new ERR_INVALID_ARG_VALUE ( 'id' , id ,
165+ 'must be a non-empty string' ) ;
166+ }
167+ requireDepth ++ ;
168+ try {
169+ return Module . _load ( id , module , /* isMain */ false ) ;
170+ } finally {
171+ requireDepth -- ;
172+ }
173+ }
174+
158175function stat ( filename ) {
159176 filename = path . toNamespacedPath ( filename ) ;
160177 if ( statCache !== null ) {
@@ -203,6 +220,15 @@ function Module(id = '', parent) {
203220 this . filename = null ;
204221 this . loaded = false ;
205222 this . children = [ ] ;
223+ let redirects ;
224+ if ( policy ?. manifest ) {
225+ const moduleURL = pathToFileURL ( id ) ;
226+ redirects = policy . manifest . getDependencyMapper ( moduleURL ) ;
227+ }
228+ setOwnProperty ( this , 'require' , makeRequireFunction ( this , redirects ) ) ;
229+ // Loads a module at the given file path. Returns that module's
230+ // `exports` property.
231+ this [ require_private_symbol ] = internalRequire ;
206232}
207233
208234const builtinModules = [ ] ;
@@ -863,6 +889,7 @@ Module._load = function(request, parent, isMain) {
863889
864890 if ( isMain ) {
865891 process . mainModule = module ;
892+ setOwnProperty ( module . require , 'main' , process . mainModule ) ;
866893 module . id = '.' ;
867894 }
868895
@@ -1053,24 +1080,6 @@ Module.prototype.load = function(filename) {
10531080 esmLoader . cjsCache . set ( this , exports ) ;
10541081} ;
10551082
1056-
1057- // Loads a module at the given file path. Returns that module's
1058- // `exports` property.
1059- Module . prototype . require = function ( id ) {
1060- validateString ( id , 'id' ) ;
1061- if ( id === '' ) {
1062- throw new ERR_INVALID_ARG_VALUE ( 'id' , id ,
1063- 'must be a non-empty string' ) ;
1064- }
1065- requireDepth ++ ;
1066- try {
1067- return Module . _load ( id , this , /* isMain */ false ) ;
1068- } finally {
1069- requireDepth -- ;
1070- }
1071- } ;
1072-
1073-
10741083// Resolved path to process.argv[1] will be lazily placed here
10751084// (needed for setting breakpoint when called with --inspect-brk)
10761085let resolvedArgv ;
@@ -1118,10 +1127,9 @@ function wrapSafe(filename, content, cjsModuleInstance) {
11181127// Returns exception, if any.
11191128Module . prototype . _compile = function ( content , filename ) {
11201129 let moduleURL ;
1121- let redirects ;
11221130 if ( policy ?. manifest ) {
11231131 moduleURL = pathToFileURL ( filename ) ;
1124- redirects = policy . manifest . getDependencyMapper ( moduleURL ) ;
1132+ policy . manifest . getDependencyMapper ( moduleURL ) ;
11251133 policy . manifest . assertIntegrity ( moduleURL , content ) ;
11261134 }
11271135
@@ -1152,18 +1160,17 @@ Module.prototype._compile = function(content, filename) {
11521160 }
11531161 }
11541162 const dirname = path . dirname ( filename ) ;
1155- const require = makeRequireFunction ( this , redirects ) ;
11561163 let result ;
11571164 const exports = this . exports ;
11581165 const thisValue = exports ;
11591166 const module = this ;
11601167 if ( requireDepth === 0 ) statCache = new SafeMap ( ) ;
11611168 if ( inspectorWrapper ) {
11621169 result = inspectorWrapper ( compiledWrapper , thisValue , exports ,
1163- require , module , filename , dirname ) ;
1170+ module . require , module , filename , dirname ) ;
11641171 } else {
11651172 result = ReflectApply ( compiledWrapper , thisValue ,
1166- [ exports , require , module , filename , dirname ] ) ;
1173+ [ exports , module . require , module , filename , dirname ] ) ;
11671174 }
11681175 hasLoadedAnyUserCJSModule = true ;
11691176 if ( requireDepth === 0 ) statCache = null ;
@@ -1339,7 +1346,7 @@ Module._preloadModules = function(requests) {
13391346 }
13401347 }
13411348 for ( let n = 0 ; n < requests . length ; n ++ )
1342- parent . require ( requests [ n ] ) ;
1349+ internalRequire ( parent , requests [ n ] ) ;
13431350 isPreloading = false ;
13441351} ;
13451352
0 commit comments