Use a Github repository branch or commit as a dependency in package.json

Recently I needed to test a branch of a forked GitHub repository inside a project. Instead of cloning the fork and symlinking the package locally, I installed the remote dependency directly into the project.

To achieve I used the following command:

Using NPM:

npm install git@github.com:user/repo.git#branchname

or

npm install git+ssh://git@github.com:user/repo.git#branchname

Using Yarn:

yarn add ssh://git@github.com:user/repo.git#branchname

💡 If you’re targeting a specific commit or tag, replace branchname with the commmithash or tagname. You can also omit the #branchname to get the default branch, typically main/master

~

With npm install there are some shorthands you can use, as it by defaults tries to look up things from GitHub when detecting the user/repo pattern:

npm install githubuser/repo
npm install github:githubuser/repo

To install things from other popular/public Git servers, change the github: to one of the supported vendors:

npm install github:githubuser/repo
npm install bitbucket:bitbucketuser/repo
npm install gitlab:gitlabuser/repo

Finally, npm install also supports installing gists from GitHub:

npm install gist:githubuser/gistid

The githubuser/ here is optional, so you can simply pass in the gistid as well.

You can read all about this in the npm install docs.

~

Did this help you out? Like what you see?
Thank me with a coffee.

I don\'t do this for profit but a small one-time donation would surely put a smile on my face. Thanks!

BuymeaCoffee (€3)

To stay in the loop you can follow @bramus or follow @bramusblog on Twitter.

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Unless noted otherwise, the contents of this post are licensed under the Creative Commons Attribution 4.0 License and code samples are licensed under the MIT License

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.