From efcb93f8b9610bff896f21ecb5add7d17de40156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 16 Dec 2022 23:41:51 +0100 Subject: fix(npm): fix require resolution if using --node-modules-dir (#17087) In our `require()` implementation we use a special logic to resolve "base path" when looking for matching packages, however this logic is in contradiction to what needs to happen if there's a local "node_modules" directory used. This commit changes require implementation to be aware if we're running off of global node modules cache or a local one. --- ext/node/02_require.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'ext/node') diff --git a/ext/node/02_require.js b/ext/node/02_require.js index 53921c242..2174ff8a9 100644 --- a/ext/node/02_require.js +++ b/ext/node/02_require.js @@ -71,6 +71,8 @@ let mainModule = null; let hasBrokenOnInspectBrk = false; let hasInspectBrk = false; + // Are we running with --node-modules-dir flag? + let usesLocalNodeModulesDir = false; function stat(filename) { // TODO: required only on windows @@ -359,10 +361,10 @@ const isRelative = ops.op_require_is_request_relative( request, ); - // TODO(bartlomieju): could be a single op - const basePath = (isDenoDirPackage && !isRelative) - ? pathResolve(curPath, packageSpecifierSubPath(request)) - : pathResolve(curPath, request); + const basePath = + (isDenoDirPackage && !isRelative && !usesLocalNodeModulesDir) + ? pathResolve(curPath, packageSpecifierSubPath(request)) + : pathResolve(curPath, request); let filename; const rc = stat(basePath); @@ -915,6 +917,9 @@ window.__bootstrap.internals = { ...window.__bootstrap.internals ?? {}, require: { + setUsesLocalNodeModulesDir() { + usesLocalNodeModulesDir = true; + }, setInspectBrk() { hasInspectBrk = true; }, -- cgit v1.2.3