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 df18ba7
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions resolvers/node/index.js
Expand Up @@ -54,6 +54,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 df18ba7

Please sign in to comment.