@@ -97,6 +97,11 @@ const { sep } = path;
97
97
const { internalModuleStat } = internalBinding ( 'fs' ) ;
98
98
const packageJsonReader = require ( 'internal/modules/package_json_reader' ) ;
99
99
const { safeGetenv } = internalBinding ( 'credentials' ) ;
100
+ const {
101
+ privateSymbols : {
102
+ require_private_symbol,
103
+ } ,
104
+ } = internalBinding ( 'util' ) ;
100
105
const {
101
106
cjsConditions,
102
107
hasEsmSyntax,
@@ -158,6 +163,20 @@ let requireDepth = 0;
158
163
let statCache = null ;
159
164
let isPreloading = false ;
160
165
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
+
161
180
function stat ( filename ) {
162
181
filename = path . toNamespacedPath ( filename ) ;
163
182
if ( statCache !== null ) {
@@ -212,6 +231,15 @@ function Module(id = '', parent) {
212
231
this . filename = null ;
213
232
this . loaded = false ;
214
233
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 ;
215
243
}
216
244
217
245
const builtinModules = [ ] ;
@@ -915,6 +943,7 @@ Module._load = function(request, parent, isMain) {
915
943
916
944
if ( isMain ) {
917
945
process . mainModule = module ;
946
+ setOwnProperty ( module . require , 'main' , process . mainModule ) ;
918
947
module . id = '.' ;
919
948
}
920
949
@@ -1099,24 +1128,6 @@ Module.prototype.load = function(filename) {
1099
1128
esmLoader . cjsCache . set ( this , exports ) ;
1100
1129
} ;
1101
1130
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
-
1120
1131
// Resolved path to process.argv[1] will be lazily placed here
1121
1132
// (needed for setting breakpoint when called with --inspect-brk)
1122
1133
let resolvedArgv ;
@@ -1180,10 +1191,9 @@ function wrapSafe(filename, content, cjsModuleInstance) {
1180
1191
// Returns exception, if any.
1181
1192
Module . prototype . _compile = function ( content , filename ) {
1182
1193
let moduleURL ;
1183
- let redirects ;
1184
1194
if ( policy ?. manifest ) {
1185
1195
moduleURL = pathToFileURL ( filename ) ;
1186
- redirects = policy . manifest . getDependencyMapper ( moduleURL ) ;
1196
+ policy . manifest . getDependencyMapper ( moduleURL ) ;
1187
1197
policy . manifest . assertIntegrity ( moduleURL , content ) ;
1188
1198
}
1189
1199
@@ -1213,18 +1223,17 @@ Module.prototype._compile = function(content, filename) {
1213
1223
}
1214
1224
}
1215
1225
const dirname = path . dirname ( filename ) ;
1216
- const require = makeRequireFunction ( this , redirects ) ;
1217
1226
let result ;
1218
1227
const exports = this . exports ;
1219
1228
const thisValue = exports ;
1220
1229
const module = this ;
1221
1230
if ( requireDepth === 0 ) statCache = new SafeMap ( ) ;
1222
1231
if ( inspectorWrapper ) {
1223
1232
result = inspectorWrapper ( compiledWrapper , thisValue , exports ,
1224
- require , module , filename , dirname ) ;
1233
+ module . require , module , filename , dirname ) ;
1225
1234
} else {
1226
1235
result = ReflectApply ( compiledWrapper , thisValue ,
1227
- [ exports , require , module , filename , dirname ] ) ;
1236
+ [ exports , module . require , module , filename , dirname ] ) ;
1228
1237
}
1229
1238
hasLoadedAnyUserCJSModule = true ;
1230
1239
if ( requireDepth === 0 ) statCache = null ;
@@ -1400,7 +1409,7 @@ Module._preloadModules = function(requests) {
1400
1409
}
1401
1410
}
1402
1411
for ( let n = 0 ; n < requests . length ; n ++ )
1403
- parent . require ( requests [ n ] ) ;
1412
+ internalRequire ( parent , requests [ n ] ) ;
1404
1413
isPreloading = false ;
1405
1414
} ;
1406
1415
0 commit comments