Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix/New] Node resolver: Try to use require.resolve when suitable #2957

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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