Skip to content

Commit d440083

Browse files
committed
[Fix/New] Node resolver: Try to use require.resolve when suitable
1 parent 7a21f7e commit d440083

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
@@ -17,6 +17,19 @@ exports.resolve = function (source, file, config) {
1717
return { found: true, path: null };
1818
}
1919

20+
// If this looks like a bare package name (not relative, not qualified
21+
// with an extension) and we're on a fresh enough version of Node.js
22+
// to have `require.resolve`, attempt that first.
23+
if (require.resolve && source.indexOf('.') === -1) {
24+
try {
25+
resolvedPath = require.resolve(source);
26+
log('Resolved to:', resolvedPath);
27+
return { found: true, path: resolvedPath };
28+
} catch (err) {
29+
log('require.resolve threw error:', err);
30+
}
31+
}
32+
2033
try {
2134
const cachedFilter = function (pkg, dir) { return packageFilter(pkg, dir, config); };
2235
resolvedPath = resolve(source, opts(file, config, cachedFilter));

0 commit comments

Comments
 (0)