Skip to content

Commit

Permalink
Merge pull request #53 from Pouja/bugfix/require-main-is-undefined
Browse files Browse the repository at this point in the history
fix: Added check if require.main is set
  • Loading branch information
inxilpro authored Aug 2, 2022
2 parents 54d06fe + 30b32b5 commit e868e55
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,15 @@ module.exports = function resolve(dirname) {

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

// Handle global bin/ directory edge-case
Expand Down

0 comments on commit e868e55

Please sign in to comment.