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 Apr 11, 2024
1 parent f77ceb6 commit d03e353
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions resolvers/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ 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 d03e353

Please sign in to comment.