Yarn Installation and Configuration Guide
- 239Words
- 1Minutes
- 26 Jul, 2024
Yarn is a popular JavaScript package management tool designed to improve the speed and reliability of dependency management. This article will provide detailed instructions on how to install Yarn on different operating systems and configure its common options.
1. Installing Yarn
Install with npm (Recommended)
If Node.js and npm are already installed on your system, you can install Yarn using the following command:
1npm install --global yarn
Install with Homebrew (macOS)
On macOS, you can use Homebrew to install Yarn:
1brew install yarn
Install with Official Script (Linux)
For Linux users, you can use the official installation script:
1curl -o- -L https://yarnpkg.com/install.sh | bash
2. Verify Installation
After installation, you can check the Yarn version to confirm if the installation was successful:
1yarn --version
3. Configuring Yarn
Configure Registry
Yarn uses the default npm registry, but you can set other registry sources to improve speed, such as using the Taobao mirror:
1yarn config set registry https://registry.npm.taobao.org
Configure Cache Directory
You can change Yarn’s cache directory:
1yarn config set cache-folder /path/to/cache
Configure Global Package Directory
If you want to install global packages to a custom directory, you can set the prefix
configuration:
1yarn config set prefix /path/to/global/node_modules
Use .yarnrc
File
You can also configure Yarn by creating or editing the .yarnrc
file in the root directory of your project:
1registry "https://registry.npmjs.org/"2cache-folder "/path/to/cache"
4. Using Yarn
Initialize a Project
Initialize a new Yarn project in your project directory:
1yarn init
Install Dependencies
Install project dependencies:
1yarn add <package-name>
Install development dependencies:
1yarn add <package-name> --dev
Update Dependencies
Update all dependencies in the project:
1yarn upgrade
Remove Dependencies
Remove dependencies that are no longer needed:
1yarn remove <package-name>