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. --- cli/node/mod.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'cli/node/mod.rs') diff --git a/cli/node/mod.rs b/cli/node/mod.rs index 64e08becb..190f386a7 100644 --- a/cli/node/mod.rs +++ b/cli/node/mod.rs @@ -391,14 +391,19 @@ static RESERVED_WORDS: Lazy> = Lazy::new(|| { pub async fn initialize_runtime( js_runtime: &mut JsRuntime, + uses_local_node_modules_dir: bool, ) -> Result<(), AnyError> { let source_code = &format!( - r#"(async function loadBuiltinNodeModules(moduleAllUrl, nodeGlobalThisName) {{ + r#"(async function loadBuiltinNodeModules(moduleAllUrl, nodeGlobalThisName, usesLocalNodeModulesDir) {{ const moduleAll = await import(moduleAllUrl); Deno[Deno.internal].node.initialize(moduleAll.default, nodeGlobalThisName); - }})('{}', '{}');"#, + if (usesLocalNodeModulesDir) {{ + Deno[Deno.internal].require.setUsesLocalNodeModulesDir(); + }} + }})('{}', '{}', {});"#, MODULE_ALL_URL.as_str(), NODE_GLOBAL_THIS_NAME.as_str(), + uses_local_node_modules_dir, ); let value = -- cgit v1.2.3