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 new package.json file. Example: yarn init -y (accept defaults)
  • yarn add <pkg> – Installs a package and records it in dependencies. Example: yarn add react
  • yarn add <pkg> --dev – Installs a package and records it in devDependencies. Example: yarn add jest --dev
  • yarn remove <pkg> – Uninstalls a package and removes it from package.json. Example: yarn remove lodash
  • yarn upgrade <pkg> – Updates a package to the latest version that satisfies the semver range. Example: yarn upgrade react
  • yarn install – Installs all dependencies listed in package.json. Example: yarn install (runs automatically after yarn init or yarn add)
  • yarn run <script> – Executes a script defined in package.json’s "scripts" section. Example: yarn run build
  • yarn start – Shortcut for yarn run start. Example: yarn start
  • yarn test – Shortcut for yarn run test. Example: yarn test
  • yarn global add <pkg> – Installs a package globally. Example: yarn global add typescript
  • yarn cache clean – Clears Yarn’s local cache. Example: yarn cache clean
  • yarn 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 list
  • yarn outdated – Shows which dependencies are out of date. Example: yarn outdated
  • yarn version – Bumps the project version and creates a Git tag. Example: yarn version --patch
  • yarn publish – Publishes the package to the registry. Example: yarn publish

Typical Beginner Workflow

  1. Create a new project
    mkdir my-app && cd my-app
    yarn init -y
  2. Add dependencies
    yarn add react react-dom
    yarn add webpack webpack-cli --dev
  3. Define scripts (edit package.json)
    "scripts": {
    "start": "webpack serve --mode development",
    "build": "webpack --mode production",
    "test": "jest"
    }
  4. Run the app
    yarn start
  5. Run tests
    yarn test
  6. Update packages
    yarn upgrade
    yarn outdated
  7. Publish (if creating a reusable library)
    yarn version --patch
    git commit -am "Bump version"
    git push
    yarn publish
  8. Global tools (optional)
    yarn global add typescript

Key Tips for Beginners

  • Use --dev for tooling (e.g., linters, test runners) so they’re not bundled in production.
  • Leverage yarn.lock to lock exact dependency versions and ensure consistent installs across machines.
  • Run yarn install after cloning a repo to get the exact versions recorded in yarn.lock.
  • Use yarn workspace if 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 dlx replaces npx for one‑off commands, and yarn config set writes to .yarnrc.yml instead of .yarnrc. If you’re starting a new project, consider using Yarn 2+ for its performance and modern features.