diff --git a/resolvers/node/index.js b/resolvers/node/index.js index 9e0e753cc7..61ec534ded 100644 --- a/resolvers/node/index.js +++ b/resolvers/node/index.js @@ -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));