Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Cannot find package 'vite' in a Yarn Plug'n'Play project

ErrorCannot find package 'vite' imported from /app/vite.config.js

Short 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.

  • Applies to Yarn 4, Node.js 22, Vite 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.json
  2. 2Match what you see, then jump to its fix

    1. 1Your application tried to access vite, but it isn't declared in your dependenciesvite is not declared in the workspace that runs itFix
    2. 2The 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. 3vite 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

What does the error look like?

  • Cannot find package 'vite' imported from /app/vite.config.js
  • Your 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.json

2Fix

From the folder of the workspace that runs Vite, add vite and each plugin the config imports as dev dependencies.

yarn add --dev vite

3Verify

Yarn now finds the binary and prints its version.

yarn run vite --version

Confirmed by yarnpkg.com

Did this fix it?

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.json

2Fix

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.js

3Verify

The dev server prints its Local address instead of the module error.

yarn dev

Confirmed by yarnpkg.com

Did this fix it?

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.yml

2Fix

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 install

3Verify

A node_modules folder with vite now exists, and the command that failed runs.

ls node_modules/vite
yarn dev

Confirmed by yarnpkg.com

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.