Yarn v2 gitignore
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Yarn v2 and newer releases changed dependency file layout, so old Yarn v1 .gitignore templates are often wrong for modern projects. The correct rules depend on one policy choice: whether your repository uses zero-install or install-on-clone. Once that policy is explicit, .gitignore becomes straightforward and stable.
Understand Yarn Berry File Roles
Yarn Berry introduces a .yarn directory with both important project files and machine-generated cache artifacts.
Common files and directories:
- '
.yarnrc.ymlconfiguration file' - '
yarn.lockdependency lock file' - '
.yarn/releasesand plugins used by project' - '
.yarn/cachepackage archives' - '
.pnp.cjsand optional.pnp.loader.mjsfor Plug and Play mode'
Some of these should be committed, some should be ignored. Treating all .yarn files the same leads to either noisy diffs or broken clones.
Pick a Dependency Policy First
Before writing rules, define team policy.
Install-on-clone policy:
- repository stores lock and Yarn config
- developers and CI run
yarn install - cache artifacts are ignored
Zero-install policy:
- repository stores package cache and Plug and Play artifacts
- fresh clone can run commands without fetching dependencies
Without this policy, .gitignore debates repeat every sprint.
Recommended .gitignore for Install-on-Clone
This setup is common in teams optimizing for smaller repositories and cleaner diffs.
This keeps repository deterministic through lock files without storing package archives.
Adjust Rules for Zero-Install
In zero-install workflows, keep cache and Plug and Play runtime files committed.
Typical changes:
- remove
.yarn/cachefrom ignore rules - track
.pnp.cjsand loader file - still ignore transient install-state files unless your team explicitly needs them
Pair this with .yarnrc.yml settings that match your linker mode.
Consistency between config and ignore rules prevents confusing dependency behavior.
Validate Ignore Behavior Explicitly
Do not guess what Git tracks. Verify with commands.
If files are already tracked from older commits, remove from index once.
Then recommit with updated ignore policy.
Monorepo and Workspace Considerations
Monorepos often mix Yarn workspaces with build tooling caches. Keep Yarn-specific rules focused, then add separate rules for framework outputs.
Examples to handle separately:
- test coverage artifacts
- bundler cache folders
- framework-specific build directories
Avoid giant copied templates. Small, intentional ignore sets are easier to maintain.
Keep CI and Docs in Sync with Ignore Policy
Your .gitignore, CI install commands, and onboarding docs should describe the same strategy.
Examples:
- install-on-clone projects should run
yarn install --immutablein CI - zero-install projects should verify cache files are present and unchanged
If docs and CI disagree, developers will commit or remove dependency artifacts unpredictably.
Common Pitfalls
- Ignoring all
.yarncontent and accidentally removing required release files. - Mixing zero-install and install-on-clone expectations in one repository.
- Updating
.gitignorebut forgetting to untrack already-committed cache files. - Copying Yarn v1 rules directly into Yarn Berry projects.
- Running CI commands that conflict with chosen dependency policy.
Summary
- Yarn v2
.gitignoredepends on clear dependency management policy. - Keep essential Yarn config tracked and ignore only policy-specific artifacts.
- Use one rule set for install-on-clone and another for zero-install.
- Validate rules with Git commands instead of assumptions.
- Align CI and documentation with the same repository policy.
Related reading
- You Don't Know JS Async and Performance - cooperative concurrency?
- 502 Bad Gateway Deploying Express Generator Template on Elastic Beanstalk
- Checking if a key exists in a JavaScript object?
- How can I change an element's class with JavaScript?
- How can I convert a string to boolean in JavaScript?
- How can I horizontally center an element?
- How can I merge properties of two JavaScript objects?
- How can I upload files asynchronously with jQuery?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.