yarn "An unexpected error occurred: … ENOENT" - Fix in CI
ENOENT means "no such file or directory". When yarn wraps it in "An unexpected error occurred", a path yarn relied on - a cache dir, a temp file, a package tarball - was missing or removed mid-run.
What this error means
yarn install aborts with "An unexpected error occurred" followed by an ENOENT for some path. It often involves the yarn cache, a temp directory, or a file a lifecycle script expected to exist.
error An unexpected error occurred:
"ENOENT: no such file or directory, open '/usr/local/share/.cache/yarn/v6/...'".Common causes
A missing or partially restored cache directory
A CI cache that restored incompletely, or a cache path that was cleaned mid-run, leaves yarn looking for files that are not there.
A lifecycle script references a non-existent path
A postinstall or prepare step that cds into or reads a file that does not exist in CI surfaces as an ENOENT inside yarn’s run.
Concurrent processes touching the same cache
Two yarn runs sharing one cache dir can delete/move files out from under each other, producing transient ENOENT.
How to fix it
Reset the cache and reinstall cleanly
Clear the yarn cache and remove node_modules before retrying.
yarn cache clean
rm -rf node_modules
yarn install --immutableIsolate and validate paths
- Give parallel jobs separate yarn cache directories.
- Make lifecycle scripts tolerant of missing optional paths.
- Verify the CI cache key restores a complete cache (or skip restoring a broken one).
How to prevent it
- Use isolated cache dirs for parallel installs.
- Validate paths in lifecycle scripts before using them.
- Bust incomplete CI caches rather than restoring them.