Skip to content

Commit

Permalink
[Fix/New] Node resolver: Try to use require.resolve when suitable
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Jan 22, 2024
1 parent 7a21f7e commit d440083
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions resolvers/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ exports.resolve = function (source, file, config) {
return { found: true, path: null };
}

// If this looks like a bare package name (not relative, not qualified
// with an extension) and we're on a fresh enough version of Node.js
// to have `require.resolve`, attempt that first.
if (require.resolve && source.indexOf('.') === -1) {
try {
resolvedPath = require.resolve(source);
log('Resolved to:', resolvedPath);
return { found: true, path: resolvedPath };
} catch (err) {
log('require.resolve threw error:', err);
}
}

try {
const cachedFilter = function (pkg, dir) { return packageFilter(pkg, dir, config); };
resolvedPath = resolve(source, opts(file, config, cachedFilter));
Expand Down

0 comments on commit d440083

Please sign in to comment.