Although most of the popular packages required by various Nodejs projects are published to the NPM registry. However, some packages would not be available to install using the NPM registry and need to download and install directly from their GitHub repository. Another reason for using GitHub is, might be you are looking to install a specific version of a package that is not available on the NPM registry.
Well, whatever the reason for directly installing NPM packages from GitHub, here in this article we learn how to do that.
Step 1: Install Git
First, we need Git installed on our Linux, Windows, or macOS where you are using NPM so that it can use by it to fetch the git repository of a package.
sudo apt install git
RPM based Linux such as RHEL.
sudo dnf install git -y
Step 2: Find the package on GitHub.
The very first step for installing an NPM package directly from GitHub is to identify the packages name that you want to get from GitHub.
This can be done by manually visiting the GitHub website and searching it. Alternatively, you can google that package to get its GitHub repository URL. In short, first, we need a link to the package repository. Once you have that move to the next steps.
Step 3: Copy the Git URL.
As you find the repository of your packages on GitHub, just copy its URL. You can easily do that by clicking on the green color button labeled with “Code” given on the top right side. Click that and from the pop-up copy the HTTPS URL provided under the “Clone” section.
For example: Let’s say I want to install a Search Index Nodejs library for GitHub. So, I will visit its page and then copy its git URL.
Step 4: Install the package using NPM.
Once you have the Git URL, we can use the regular ‘NPM install‘ command to get GitHub’s NPM package on our system. First, in your command terminal or command prompt navigate to the project directory where you want to install the package and then use the given command syntax to get it.
npm install link-to-GitHub-repository
npm install https://github.com/fergiemcdowall/search-index.git
Step 5: Verify the installation.
Once you have executed the command, the NPM package manager will download and install the GitHub NPM package you want on your system.
To verify that, whether actually this worked and whether you have the package you want or not, run the given command.
This will list all the Nodejs packages installed for your project including the one you got from GitHub.
You would have realized that it is not a difficult process to install an NPM package directly from GitHub. By following just a few steps, you can easily get the packages that are not available in the NPM registry or any specific version you require.