Delving Developer

How to use a GitHub repository as an NPM dependency

Eddie Cunningham
Eddie Cunningham
2 min readVersion Control
Cover Image for How to use a GitHub repository as an NPM dependency

Recently, I was reading through the documentation of a GitHub repository. I noticed some features included that didn't seem to exist in the version that was released to NPM. I was too eager to wait for a release, I wanted this new feature now! So I installed the package directly from GitHub and used it that way until an official release was created. In this article, I'll explain how I did it, and some other nifty commands that you might need.

Important note: All repositories installed via these methods must have an adequate package.json in their root. Without this. the installation will fail!

Installing public packageslink

Here's how to install an NPM package from GitHub, using either yarn or npm:

# yarn
yarn add <GitHub user name>/<GitHub repository name>

# npm
npm install <GitHub user name>/<GitHub repository name>

Here's how to install an NPM package from a specific branch or commit:

# yarn
yarn add<GitHub user name>/<GitHub repository name>#<branch/commit>

# npm
npm install <GitHub user name>/<GitHub repository name>#<branch/commit>

We can also install an NPM package from a specific version or tag:

# yarn
yarn add <GitHub user name>/<GitHub repository name>@<version/tag> --exact

# npm
npm install <GitHub user name>/<GitHub repository name>@<version/tag> --exact

Installing private packageslink

Sometimes you'll want to install a package that is only available in a private repository. To do so, you'll need to use the SSH Github link. If you haven't got SSH set up on your device, you can follow this handy guide by Github. This process can be applied to Bitbucket, GitLab and others.

# yarn
yarn add git+ssh://git@github.com:<GitHub user name>/<GitHub repository name>.git

# npm
yarn add git+ssh://git@github.com:<GitHub user name>/<GitHub repository name>.git

Install packages from GitLab or Bitbucketlink

If you're looking to install dependencies from GitLab or Bitbucket instead, npm offers a very simple way to do this out of the box. You can simply prefix your commands with gitlab: or bitbucket:

# GitLab
yarn add gitlab:<GitLab user name>/<GitLab repository name>

# Bitbucket
npm install bitbucket:<Bitbucket user name>/<Bitbucket repository name>

However, Yarn doesn't support this, and you'll have to utilize SSH instead:

# GitLab
yarn add git+ssh://git@gitlab.com:<GitLab user name>/<GitLab repository name>.git

# Bitbucket
yarn add git+ssh://git@bitbucket.org/<Bitbucket user name>/<Bitbucket repository name>.git

Installing from Gistslink

You can also install directly from a gist. If there is a package.json that is correctly configured you'll be able to take the ID from the URL of the Gist and append it to the following command:

# yarn
yarn add gist:<Gist ID>

#npm
npm install gist:<Gist ID>