Cannot find package 'vite' in a Yarn Plug'n'Play project
Cannot find package 'vite' imported from /app/vite.config.jsShort answer
Yarn Plug'n'Play has no node_modules folder. Node finds vite only through the loader Yarn injects, and only for packages the current workspace declares. The error appears when vite is not a dependency of that workspace, when Vite is started outside Yarn, or when a tool needs a real node_modules folder.
Fix it fast
1Find out which cause you have
Run Vite through Yarn and read the first error line. If the version prints, Yarn can see vite and the problem is how the failing command is started. Then check what the workspace you run in declares.
yarn run vite --version grep -n -E '"(vite|@vitejs/[^"]+)"' package.json2Match what you see, then jump to its fix
- 1
Your application tried to access vite, but it isn't declared in your dependenciesvite is not declared in the workspace that runs itFix - 2
The error shows up with plain node, a global vite, an IDE run button or a CI step, but not with yarn devVite is started outside YarnFix - 3
vite is declared and yarn run vite --version works, but another tool or package still cannot find modulesA tool in the chain needs a real node_modules folderFix
- 1
What does the error look like?
Cannot find package 'vite' imported from /app/vite.config.jsYour application tried to access vite, but it isn't declared in your dependencies
Cause 1 of 3
vite is not declared in the workspace that runs it
1Check
PnP lets a package require only what it lists in its own dependencies, and yarn run finds a binary only when a dependency of the current workspace exposes it. In a monorepo, vite listed only in the root or in a sibling workspace does not count. The same holds for vite.config.js: every package it imports, plugins included, must be declared by that workspace.
grep -n -E '"(vite|@vitejs/[^"]+)"' package.json2Fix
From the folder of the workspace that runs Vite, add vite and each plugin the config imports as dev dependencies.
yarn add --dev vite3Verify
Yarn now finds the binary and prints its version.
yarn run vite --versionConfirmed by yarnpkg.com
Cause 2 of 3
Vite is started outside Yarn
1Check
With PnP there is no node_modules folder for Node to search. Yarn injects PnP into the processes it launches, which is why it ships yarn node: plain node would not do that. Compare how the failing command starts with how your scripts start Vite.
grep -n -A4 '"scripts"' package.json2Fix
Start everything through Yarn: a script that calls vite by name, run with yarn, or yarn node for a Node script. Point the IDE and CI at the same command.
// package.json
"scripts": {
"dev": "vite",
"build": "vite build"
}
yarn dev
yarn node scripts/prepare.js3Verify
The dev server prints its Local address instead of the module error.
yarn devConfirmed by yarnpkg.com
Cause 3 of 3
A tool in the chain needs a real node_modules folder
1Check
Some tools expect a classic node_modules folder, and some packages reach for modules they do not declare. The Yarn docs name React Native and Expo as tools that need typical node_modules installs. Check which linker the project uses.
grep -n "nodeLinker\|pnpMode" .yarnrc.yml2Fix
Switch the linker to node-modules and install again. If only a few packages reach for undeclared modules, pnpMode: loose is a milder choice: it lets packages use whatever a classic install would have hoisted.
# .yarnrc.yml
nodeLinker: node-modules
# then
yarn install3Verify
A node_modules folder with vite now exists, and the command that failed runs.
ls node_modules/vite
yarn devConfirmed by yarnpkg.com
How do I stop it coming back?
- Declare every package that your own code or config file imports; PnP will not let you rely on one that another package happened to install.
- Run scripts, CI steps and IDE tasks through yarn, so that the same resolution applies everywhere.
- Keep .yarnrc.yml in the repository, so that everyone on the team uses the same linker.
- In a monorepo, add vite to each workspace that runs it, not only to the root.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Corepack does not use the yarn version set in package.json
the packageManager field is ignored
Corepack only picks the yarn version from the packageManager field when its own yarn shim is the one that runs.
4 causes, each with a fixBack end
Yarn cannot find the release file named in yarnPath
Cannot find module .yarn/releases/yarn-3.x.x.cjs
Yarn 2 and newer can run a copy of itself stored in the project, named by yarnPath in .yarnrc.yml.
2 causes, each with a fixBack end
npm run dev cannot find Vite: Cannot find module vite\bin\vite.js
Error: Cannot find module 'C:\foo\bar&baz\vite\bin\vite.js'
The dev script runs the vite binary from node_modules.
2 causes, each with a fixBack end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fix