Vitest error
Failed to resolve import "@/lib/utils" from "src/App.tsx". Does the file exist?
Failed to resolve import "@/lib/utils" from "src/App.tsx". Does the file exist? is the vite:import-analysis plugin saying Vitest cannot map the @/ prefix to a folder. The Vitest docs say Vite does not take tsconfig.json into account by default, so a paths entry that your editor understands does nothing in the test run. Add resolve.alias with an absolute path to src to vitest.config.ts, or the vite-tsconfig-paths plugin, or resolve.tsconfigPaths: true. With the default node environment the same missing alias prints Cannot find package instead. Reproduced with vitest 5.0.5 on 2026-10-31.
What does the error look like in the terminal?
With the jsdom environment, our run printed the message below, naming the importing file, the plugin and the line. With no environment set, Vitest ran in node and the same missing alias printed "Error: Cannot find package '@/lib/greet' imported from" the importing file. Both mean the prefix was not mapped.
Error: Failed to resolve import "@/lib/greet" from "src/lib/x.ts". Does the file exist?
Plugin: vite:import-analysis
File: /path/to/app/src/lib/x.ts:1:22
1 | import { greet } from "@/lib/greet";
| ^Which fix should I use: resolve.alias, vite-tsconfig-paths or resolve.tsconfigPaths?
All three made the test pass in our run with Vite 8.3.5 and vite-tsconfig-paths 6.1.1. The Vite docs describe resolve.tsconfigPaths as off by default (false) and as using the paths option of tsconfig.json to resolve imports. With the plugin installed on that Vite version, a note in the terminal said Vite supports tsconfig paths natively through resolve.tsconfigPaths. The Next.js guide for Vitest still lists the plugin. With resolve.alias, the Vite docs say to use absolute paths, because relative alias values are used as-is and not resolved into file system paths.
import path from "node:path"
import { defineConfig } from "vitest/config"
export default defineConfig({
resolve: {
alias: { "@": path.resolve(import.meta.dirname, "src") },
},
test: { environment: "jsdom" },
})The alias is in vite.config.ts, so why does Vitest not see it?
The Vitest config docs say that when a vitest.config file exists it has the higher priority and all options in vite.config are ignored. Copy the alias into vitest.config, or keep only one file. The test.alias option is merged with resolve.alias, so either place works once Vitest reads that file.
Checklist
How to fix it, then check the fix
- 01Find the paths entry and every Vite or Vitest config:
grep -rn --include="tsconfig*.json" --exclude-dir=node_modules -A2 "paths" . ls vite.config.* vitest.config.* 2>/dev/null - 02Pick one fix and put it in the file Vitest reads: vitest.config if it exists, else vite.config.
- 03For the plugin, install it as a dev dependency and add it under plugins. Or set resolve.tsconfigPaths to true on a Vite version that has it:
npm install -D vite-tsconfig-paths - 04If you use resolve.alias, point it at an absolute path built with path.resolve, not at "./src".
- 05Run the failing file. A new error that names another prefix means that alias is missing too:
npx vitest run path/to/file.test.ts
Related
Related guides
Sources
Official docs behind this checklist
Browse all launch guides · Read resources · How to make and check your next change
FAQ
FAQ for builders before launch
Why does the app build but Vitest cannot resolve @/?
Vitest reads vitest.config, or vite.config when there is none, and per its docs Vite ignores tsconfig.json unless asked. An alias that exists only in tsconfig.json is invisible to the test run.
Do I still need vite-tsconfig-paths?
Not on a Vite version with resolve.tsconfigPaths, as our run showed, but the plugin still worked there and the Next.js guide still installs it. Pick one and do not set both.
The alias is set and the error remains. What else can it be?
The message asks whether the file exists, so check that the folder the alias points to exists and that the import path matches a real file. Also check that the alias value is absolute, as Vite uses relative values as-is.