From 6c1fc6cb57769fd713553a8ae0213801025414da Mon Sep 17 00:00:00 2001 From: MukundaKatta Date: Tue, 14 Apr 2026 13:16:44 -0700 Subject: [PATCH] fix: ignore paths.base under Vitest to avoid vite-node module resolution failures When kit.paths.base is a prefix of the project's filesystem path (e.g. "/Users"), Vite strips that prefix from vite-node's HTTP module URLs, breaking module resolution with ERR_MODULE_NOT_FOUND. Force base to '/' when process.env.VITEST is set. Fixes #13737 --- .changeset/fix-vitest-base-path-conflict.md | 7 +++++++ packages/kit/src/exports/vite/index.js | 7 ++++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 .changeset/fix-vitest-base-path-conflict.md diff --git a/.changeset/fix-vitest-base-path-conflict.md b/.changeset/fix-vitest-base-path-conflict.md new file mode 100644 index 000000000000..d05cf1e1dd25 --- /dev/null +++ b/.changeset/fix-vitest-base-path-conflict.md @@ -0,0 +1,7 @@ +--- +'@sveltejs/kit': patch +--- + +fix: don't apply `paths.base` to Vite config when running under Vitest + +When `kit.paths.base` was set to a prefix of the project's filesystem path (e.g. `/Users`), Vite would strip that prefix from the URLs vite-node uses to fetch modules over HTTP, causing `ERR_MODULE_NOT_FOUND` failures. Under Vitest, `base` is now forced to `/` so module resolution works regardless of the configured base path. diff --git a/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js index fe829e73e240..ccf6bf340c52 100644 --- a/packages/kit/src/exports/vite/index.js +++ b/packages/kit/src/exports/vite/index.js @@ -988,7 +988,12 @@ async function kit({ svelte_config }) { } else { new_config = { appType: 'custom', - base: kit_paths_base, + // When running under Vitest, don't apply kit.paths.base — vite-node + // serves filesystem modules over HTTP and Vite will strip the base + // prefix from those URLs, breaking module resolution when the base + // happens to match the start of the project path (e.g. "/Users"). + // See https://github.com/sveltejs/kit/issues/13737 + base: process.env.VITEST ? '/' : kit_paths_base, build: { rollupOptions: { // Vite dependency crawler needs an explicit JS entry point