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