When using Yarn, installing packages from GitHub repositories is a common requirement, especially when the package you need is not available in the npm registry or when you need to install a specific version or branch. Below are the steps to install packages from GitHub repositories in Yarn:
Step 1: Find the GitHub Repository URL
First, identify the URL of the GitHub repository you want to install. For example, suppose you want to install from the following GitHub repository:
shellhttps://github.com/user/repo
Step 2: Add the Dependency with Yarn
You can directly use the add command of Yarn to add the GitHub repository as a dependency. The format is:
shellyarn add <github-username>/<repository-name>
For example, to install from the repository mentioned above:
shellyarn add user/repo
Step 3: Specify Branch, Tag, or Commit
If you need to specify a particular branch, tag, or commit, append # followed by the identifier to the repository name. For example:
- To install a specific branch:
shell
yarn add user/repo#branch-name
shell- To install a specific tag:
yarn add user/repo#v1.0.0
shell- To install a specific commit:
yarn add user/repo#a1b2c3d
shell### Step 4: Verify Installation After installation, you can see the added dependency in your project's `package.json` file. Additionally, you can run:
yarn list
shellto view the list of installed packages and confirm that your package is correctly installed. ### Example Suppose you are developing a Node.js application that depends on a GitHub library named `example-lib`, maintained by user `example-user`. You need to install the `develop` branch of this library. You can use the following command to install it:
yarn add example-user/example-lib#develop
shellThis will add the `develop` branch of the `example-lib` library to your project's dependencies.