Skip to content

fix: use node_modules/.vite as cacheDir when node_modules exists - #21777

Merged
sapphi-red merged 11 commits into
vitejs:mainfrom
Xavrir:fix/cachedir-node-modules-21768
Jun 2, 2026
Merged

sapphi-red merged 11 commits into
vitejs:mainfrom
Xavrir:fix/cachedir-node-modules-21768

Conversation

@Xavrir

@Xavrir Xavrir commented Mar 7, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR fixes issue #21768 by checking for the existence of the directory when determining the default cache directory, not just when exists.

Changes

  • Modified to check for directory existence
  • Added tests in to verify the new behavior

Behavior

The cacheDir resolution now follows this priority:

  1. If config.cacheDir is explicitly set → use it
  2. If package.json exists → use node_modules/.vite in that directory
  3. NEW: If node_modules directory exists → use node_modules/.vite
  4. Otherwise → use .vite in the project root

This fixes caching for Deno projects that create node_modules without package.json.

Testing

Added two test cases:

  • Test that node_modules/.vite is used when node_modules exists without package.json
  • Test that .vite is used when neither package.json nor node_modules exist

Closes #21768

Fallback to node_modules/.vite when the directory exists, even without
package.json. This fixes caching for Deno projects with nodeModulesDir.

Closes vitejs#21768
@hendrik-huels

Copy link
Copy Markdown

Is the second check necessary? It seems like it is already covered by the third check.

- Move fs.existsSync into the fallback branch so it only runs when needed
- Restructure ternary to if/else for readability
- Add comment explaining the pkgDir vs resolvedRoot/node_modules distinction
- Fix tests to use /tmp and pass root via inlineConfig
@Xavrir

Xavrir commented Mar 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Not redundant, findNearestPackageData walks up the directory tree so pkgDir can be a parent dir (monorepos etc). The new nodeModulesDirExists check only kicks in when there's no package.json found at all, which is the actual #21768 case (e.g. Deno projects with node_modules/ but no package.json).

Restructured it to make that clearer in fd546e0, moved the existsSync into the fallback branch so it only runs when needed and added a short comment.

Xavrir added 2 commits March 8, 2026 04:43
path.resolve on Windows returns backslashes, but config.cacheDir is
normalized to forward slashes. Wrap expected values in normalizePath
to match on all platforms.

@bluwy bluwy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great 👍

@Xavrir
Xavrir requested a review from bluwy March 11, 2026 23:11
@Xavrir

Xavrir commented Mar 11, 2026 •

Copy link
Copy Markdown
Contributor Author

CI is green now,Ready to merge whenever convenient. Thanks for the review @bluwy!

bluwy
bluwy previously approved these changes Mar 13, 2026
@sapphi-red sapphi-red added this to the 8.1 milestone Mar 16, 2026
sapphi-red
sapphi-red previously approved these changes Mar 16, 2026

@sapphi-red sapphi-red left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Let's merge this in a minor rather than a patch, just in case, as it changes the default path (in some cases).

@sapphi-red
sapphi-red dismissed stale reviews from bluwy and themself via f005798 June 2, 2026 04:13
@sapphi-red
sapphi-red merged commit 4417b4f into vitejs:main Jun 2, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use node_modules/.vite as default cacheDir when a node_modules directory exists

4 participants