Skip to content

Commit 30b32b5

Browse files
author
Pouja Nikray
committed
fix: Added check if require.main is set
When using commonjs2 then require.main is undefined.
1 parent 54d06fe commit 30b32b5

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lib/resolve.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,15 @@ module.exports = function resolve(dirname) {
107107

108108
// If the above didn't work, or this module is loaded globally, then
109109
// resort to require.main.filename (See http://nodejs.org/api/modules.html)
110-
if (alternateMethod || null == appRootPath) {
111-
appRootPath = path.dirname(requireFunction.main.filename);
110+
if ((alternateMethod || null == appRootPath)) {
111+
if (requireFunction.main) {
112+
appRootPath = path.dirname(requireFunction.main.filename);
113+
} else {
114+
// This is the case when app-root-path is bundle'd to a commonjs2 format and is being called from an esm file.
115+
// In those cases require.main is undefined (See https://nodejs.org/api/modules.html#accessing-the-main-module)
116+
// At that point we can only get the root from looking at the callee
117+
appRootPath = path.dirname(process.argv[1]);
118+
}
112119
}
113120

114121
// Handle global bin/ directory edge-case

0 commit comments

Comments
 (0)