Skip to content
Latchkey

Vitest "Failed to resolve import" - Fix Module Resolution in CI

Vite (under Vitest) could not resolve an import. The package is missing, an alias is not configured in the Vite/Vitest config, or the path case does not match the real file on a case-sensitive Linux runner.

What this error means

A test file fails to load with "Failed to resolve import '@/lib/db' from 'src/x.test.ts'. Does the file exist?" It often passes on macOS but fails on Linux CI, or after a dependency was dropped.

vitest
Error: Failed to resolve import "@/lib/db" from "src/users.test.ts".
Does the file exist?

  Plugin: vite:import-analysis

Common causes

Alias not configured for Vitest

A @/... alias defined for the app build is not present in the Vite/Vitest resolve.alias, so the import is unresolvable during tests.

Missing dependency or wrong path case

The package was never installed (or pruned by --omit=dev), or the import case does not match the real filename on a case-sensitive filesystem.

How to fix it

Configure the alias in Vitest

Add resolve.alias (or reuse the Vite config) so Vitest resolves aliases like the build does.

vitest.config.ts
// vitest.config.ts
import { defineConfig } from 'vitest/config';
import { fileURLToPath } from 'node:url';
export default defineConfig({
  resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } },
});

Fix the path case and install deps

  1. Match the import path to the real filename exactly, including case.
  2. Run npm ci so test-only dependencies are present.
  3. Reuse the app's Vite config in Vitest to keep aliases in sync.

How to prevent it

  • Share one Vite config between the app and Vitest.
  • Lint imports and develop on a case-sensitive filesystem.
  • Commit the lockfile so installs are reproducible.

Frequently asked questions

What causes ""Failed to resolve import""?
A @/... alias defined for the app build is not present in the Vite/Vitest resolve.alias, so the import is unresolvable during tests.
How do I fix "Failed to resolve import"?
Add resolve.alias (or reuse the Vite config) so Vitest resolves aliases like the build does.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card