Skip to content

Commit 2223dc0

Browse files
dgobaudKehrlann
authored andcommitted
fix crash when using from cli
require.main is undefined when running from cli - don't use unless it exists
1 parent 07a6b80 commit 2223dc0

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ function addPath (path) {
8282
if (modulePaths.indexOf(path) === -1) {
8383
modulePaths.push(path)
8484
// Enable the search path for the current top-level module
85-
addPathHelper(path, require.main.paths)
85+
if (require.main) {
86+
addPathHelper(path, require.main.paths)
87+
}
8688
parent = module.parent
8789

8890
// Also modify the paths of the module that was used to load the
@@ -115,7 +117,9 @@ function addAlias (alias, target) {
115117
function reset () {
116118
// Reset all changes in paths caused by addPath function
117119
modulePaths.forEach(function (path) {
118-
removePathHelper(path, require.main.paths)
120+
if (require.main) {
121+
removePathHelper(path, require.main.paths)
122+
}
119123

120124
// Delete from require.cache if the module has been required before.
121125
// This is required for node >= 11

0 commit comments

Comments
 (0)