Getting Started
Basic Yarn commands and workflow for beginners
tutorial
Yarn is a fast, reliable, and secure package manager for JavaScript projects. It offers a streamlined workflow for installing dependencies, running scripts, and publishing packages. Below is a beginner‑friendly overview of the most common Yarn commands and a typical workflow for starting, developing, and publishing a project.
Core Commands
yarn init– Creates a newpackage.jsonfile. Example:yarn init -y(accept defaults)yarn add <pkg>– Installs a package and records it independencies. Example:yarn add reactyarn add <pkg> --dev– Installs a package and records it indevDependencies. Example:yarn add jest --devyarn remove <pkg>– Uninstalls a package and removes it frompackage.json. Example:yarn remove lodashyarn upgrade <pkg>– Updates a package to the latest version that satisfies the semver range. Example:yarn upgrade reactyarn install– Installs all dependencies listed inpackage.json. Example:yarn install(runs automatically afteryarn initoryarn add)yarn run <script>– Executes a script defined inpackage.json’s"scripts"section. Example:yarn run buildyarn start– Shortcut foryarn run start. Example:yarn startyarn test– Shortcut foryarn run test. Example:yarn testyarn global add <pkg>– Installs a package globally. Example:yarn global add typescriptyarn cache clean– Clears Yarn’s local cache. Example:yarn cache cleanyarn config set <key> <value>– Sets a Yarn configuration value. Example:yarn config set registry https://registry.npmjs.org/yarn list– Lists installed packages and their dependencies. Example:yarn listyarn outdated– Shows which dependencies are out of date. Example:yarn outdatedyarn version– Bumps the project version and creates a Git tag. Example:yarn version --patchyarn publish– Publishes the package to the registry. Example:yarn publish
Typical Beginner Workflow
- Create a new project
mkdir my-app && cd my-appyarn init -y - Add dependencies
yarn add react react-domyarn add webpack webpack-cli --dev - Define scripts (edit
package.json)"scripts": {"start": "webpack serve --mode development","build": "webpack --mode production","test": "jest"} - Run the app
yarn start - Run tests
yarn test - Update packages
yarn upgradeyarn outdated - Publish (if creating a reusable library)
yarn version --patchgit commit -am "Bump version"git pushyarn publish - Global tools (optional)
yarn global add typescript
Key Tips for Beginners
- Use
--devfor tooling (e.g., linters, test runners) so they’re not bundled in production. - Leverage
yarn.lockto lock exact dependency versions and ensure consistent installs across machines. - Run
yarn installafter cloning a repo to get the exact versions recorded inyarn.lock. - Use
yarn workspaceif you’re working on a monorepo; it lets you share dependencies and run scripts across packages. - Yarn 2+ (Berry) introduces Plug’n’Play, workspaces, and a different configuration system. Most of the commands above work the same, but some flags and the default behavior differ. For example,
yarn dlxreplacesnpxfor one‑off commands, andyarn config setwrites to.yarnrc.ymlinstead of.yarnrc. If you’re starting a new project, consider using Yarn 2+ for its performance and modern features.