@@ -82,12 +82,15 @@ function addPath (path) {
82
82
if ( modulePaths . indexOf ( path ) === - 1 ) {
83
83
modulePaths . push ( path )
84
84
// Enable the search path for the current top-level module
85
- addPathHelper ( path , require . main . paths )
85
+ var mainModule = getMainModule ( )
86
+ if ( mainModule ) {
87
+ addPathHelper ( path , mainModule . paths )
88
+ }
86
89
parent = module . parent
87
90
88
91
// Also modify the paths of the module that was used to load the
89
92
// app-module-paths module and all of it's parents
90
- while ( parent && parent !== require . main ) {
93
+ while ( parent && parent !== mainModule ) {
91
94
addPathHelper ( path , parent . paths )
92
95
parent = parent . parent
93
96
}
@@ -113,9 +116,13 @@ function addAlias (alias, target) {
113
116
* The function is undocumented and for testing purposes only
114
117
*/
115
118
function reset ( ) {
119
+ var mainModule = getMainModule ( )
120
+
116
121
// Reset all changes in paths caused by addPath function
117
122
modulePaths . forEach ( function ( path ) {
118
- removePathHelper ( path , require . main . paths )
123
+ if ( mainModule ) {
124
+ removePathHelper ( path , mainModule . paths )
125
+ }
119
126
120
127
// Delete from require.cache if the module has been required before.
121
128
// This is required for node >= 11
@@ -126,7 +133,7 @@ function reset () {
126
133
} )
127
134
128
135
var parent = module . parent
129
- while ( parent && parent !== require . main ) {
136
+ while ( parent && parent !== mainModule ) {
130
137
removePathHelper ( path , parent . paths )
131
138
parent = parent . parent
132
139
}
@@ -205,6 +212,10 @@ function init (options) {
205
212
}
206
213
}
207
214
215
+ function getMainModule ( ) {
216
+ return require . main . _simulateRepl ? undefined : require . main
217
+ }
218
+
208
219
module . exports = init
209
220
module . exports . addPath = addPath
210
221
module . exports . addAlias = addAlias
0 commit comments