Skip to content

Commit 1592741

Browse files
committed
[Fix/New] Node resolver: Try to use require.resolve when suitable
1 parent 6554bd5 commit 1592741

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

resolvers/node/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ exports.resolve = function (source, file, config) {
5454
return { found: true, path: null };
5555
}
5656

57+
// If this looks like a bare package name (not relative, not qualified
58+
// with an extension) and we're on a fresh enough version of Node.js
59+
// to have `require.resolve`, attempt that first.
60+
if (require.resolve && source.indexOf('.') === -1) {
61+
try {
62+
resolvedPath = require.resolve(source);
63+
log('Resolved to:', resolvedPath);
64+
return { found: true, path: resolvedPath };
65+
} catch (err) {
66+
log('require.resolve threw error:', err);
67+
}
68+
}
69+
5770
try {
5871
const cachedFilter = function (pkg, dir) { return packageFilter(pkg, dir, config); };
5972
resolvedPath = resolve(source, opts(file, config, cachedFilter));

0 commit comments

Comments
 (0)