Common Yarn Commands

Yarn is a high-performance Node.js package manager developed by Facebook, designed to address the pain points of npm regarding dependency installation speed and conflict resolution. As one of the standard tools in modern frontend development, mastering common Yarn commands is crucial for efficiently managing project dependencies. This article systematically reviews core Yarn operations to help you get started quickly and improve your development efficiency.

Initialization and Project Creation

At the start of a project, first select an appropriate package manager template based on specific requirements. Yarn offers various preset schemes that can generate directory structures with basic configuration files in one click.

  • Using Vite Template: Suitable for building modern, fast web applications.
    yarn create vite my-app --template react
    cd my-app
    yarn install
    
  • Using Next.js Template: Ideal for scenarios requiring Server-Side Rendering (SSR) or Static Site Generation (SSG).
    yarn create next-app my-next-site
    cd my-next-site
    yarn add react-dom
    

Upon completion of initialization, Yarn automatically generates a package.json file in the root directory, containing project metadata, dependency declarations, and script configurations.

Dependency Installation and Management

This is one of Yarn's core features; its advantages lie in extremely high installation speeds and strict dependency resolution capabilities.

  • Install Local Dependencies: Add specified packages to the current project.
    yarn add <package-name>
    # Example: Install React and ReactDOM
    yarn add react react-dom
    
  • Install Development Dependencies: Used only for local development; not deployed to production environments.
    yarn add -D <package-name>
    # Example: Install Jest testing framework
    yarn add -D jest @types/jest
    
  • Install Specific Versions: Precisely control dependency versions to avoid conflicts.
    yarn add <package-name>@<version>
    # Example: Install lodash v4.17.21
    yarn add lodash@4.17.21
    
  • View Dependency Tree: Analyze all dependencies in the project and identify potential conflicts.
    yarn why <package-name>
    # Output will display the complete chain of how this package is depended upon by others
    

Version Locking and Synchronization

Yarn introduces a yarn.lock file to lock specific versions of all dependencies, ensuring that different developers or CI/CD environments build an identical project environment. This function is similar to npm's package-lock.json, but Yarn performs more rigorous deduplication and optimization during the resolution process.

  • Generate Lock File: Automatically created upon first dependency installation (if not manually specified).

    yarn install --frozen-lockfile
    

    This command attempts to install using exact versions recorded in yarn.lock without modifying package.json. If the lock file does not exist or version conflicts occur, it will error and prompt for an update.

  • Clean Cache: Resolve installation failures caused by caching issues.

    yarn cache clean
    # Or clean cache for a specific package
    yarn cache clean <package-name>
    

Project Script Execution

The scripts field in package.json allows us to encapsulate common commands into convenient entry points, and Yarn provides dedicated commands to execute these scripts.

  • Run All Scripts: Execute all tasks defined in scripts sequentially.
    yarn run
    # Example output: First executes build, then lint
    yarn run
    
  • Execute Specific Script: Specify the task name to be executed via parameters.
    yarn <script-name>
    # Example: Run build command
    yarn build
    # Or run test command
    yarn test --watchAll
    
  • Run Multiple Scripts in Parallel: Start multiple independent tasks simultaneously to improve development efficiency.
    yarn run parallel <script1> <script2> ...
    # Example: Compile TypeScript and check code style simultaneously
    yarn run parallel tsc eslint
    

Dependency Updates and Maintenance

With the rapid development of the open-source ecosystem, project dependencies often need regular updates to obtain security patches or new features. Yarn provides various strategies to manage this process.

  • Upgrade a Single Package: Upgrade the version of a specific package to the latest available version.
    yarn upgrade <package-name>
    # Example: Upgrade lodash to the latest version
    yarn upgrade lodash
    
  • Upgrade All Development Dependencies: Batch update local dependencies such as testing and build tools.
    yarn upgrade-interactive --latest-major
    # Interactive interface allowing confirmation of individual upgrades
    
  • Downgrade Dependency: When a new version introduces breaking changes, you can roll back to an older version.
    yarn add <package-name>@<old-version>
    # Example: Rollback React to v17.0.2
    yarn add react@17.0.2
    

Best Practices Recommendations

Following the habits listed below can significantly improve team collaboration and deployment stability in actual development. First, always run yarn install --frozen-lockfile after modifying dependencies in package.json to ensure strict consistency between the lock file and installed versions. Second, use the yarn why command to analyze unnecessary dependency packages in large projects for timely removal, reducing bundle size. Finally, for production environments, it is recommended to configure a .yarnrc.yml file to enable performance optimization options, such as using Yarn Berry's virtual repository feature to accelerate download speeds. By mastering the commands above, you will be able to handle various complex dependency management scenarios with confidence like a professional engineer.