3.5 KiB
Yarn Basics Cheat Sheet (Beginner)
This page explains Yarn from zero and gives a short command set you can use daily.
1) What Yarn Is
Yarn is a JavaScript/TypeScript package manager.
It helps you:
- Install libraries your project depends on.
- Keep dependency versions consistent.
- Run scripts defined by the project.
- Work with private package registries.
2) Core Concepts
Package
A package is reusable code published to a registry (for example npm or your company Gitea registry).
Example in this project context:
@jig-software/trest
Dependency
A dependency is a package your app needs to run.
- Regular dependency: needed at runtime.
- Dev dependency: needed for development/testing only.
package.json
This file declares project metadata and dependencies.
yarn.lock
This lock file pins exact dependency versions to keep installs reproducible across machines and CI.
Registry
A registry is where packages are downloaded from.
- Public: npmjs
- Private: your company Gitea Packages endpoint
Scope
A scope is a package namespace like @jig-software.
A scope can be routed to a custom registry in .yarnrc.yml.
.yarnrc.yml
Project-level Yarn config. It controls behavior such as:
- Registry URLs
- Authentication token source
- Install/link strategy
PnP (Plug'n'Play)
PnP is Yarn's alternative to node_modules.
- Faster and stricter dependency resolution.
- Dependencies are mapped through
.pnp.cjs. - Some tools that assume
node_modulesmay need extra config.
3) How Your Current .yarnrc.yml Works
From this repo config:
nodeLinker: pnpmeans no traditionalnode_modulestree.npmScopes.jig-softwareroutes@jig-software/*packages to Gitea.npmAuthTokenreads token from env varJIG_SOFTWARE_GITEA_DEPLOYMENT_TOKEN.npmAlwaysAuth: truealways sends auth to that private registry.
4) 8 Commands You Need Day-to-Day
- Check Yarn version
yarn --version
- Install all dependencies from lockfile
yarn install
- Add a dependency
yarn add <package-name>
- Add a dev dependency
yarn add -D <package-name>
- Remove a dependency
yarn remove <package-name>
- Run a package script
yarn <script-name>
- Inspect package info in registry
yarn npm info @jig-software/trest
- Install trest from private scope
yarn add @jig-software/trest
5) Private Registry Quick Start (This Company)
- Export token in shell:
export JIG_SOFTWARE_GITEA_DEPLOYMENT_TOKEN="<your-gitea-token>"
- Verify package access:
yarn npm info @jig-software/trest
- Install package:
yarn add @jig-software/trest
6) Common Errors and Fixes
Error: 401 Unauthorized
Likely causes:
- Missing or expired token.
- Wrong token scopes.
Fix:
- Re-export token and retry.
- Regenerate PAT with package read access.
Error: 404 on whoami
Some Gitea npm endpoints do not support whoami.
Fix:
- Use
yarn npm info @jig-software/trestas the verification command.
Error: tool expects node_modules
Cause:
- Project uses PnP.
Fix:
- Configure that tool for PnP, or switch linker mode for that project if approved by the team.
7) Mental Model
Use this flow every time:
- package.json asks for packages.
- yarn.lock pins exact versions.
- .yarnrc.yml tells Yarn where/how to fetch.
- Yarn installs and links dependencies (PnP here).
- Scripts run with the installed dependency graph.
If one step fails, debug that layer first.