GitHub hosted NPM package

Tom Szpytman
Feb 14 · 2 min read

As a team (or individual), you might have multiple projects and have ended up down the path of collecting shared utilities and components in a common library. You need to this library to be versioned and available for all of your projects to reference within their package.json files. Your common library is still a POC and you don’t want to dive straight into spending time setting up the infrastructure / processes needed to create a fully-fledged NPM package.

Fear not. There’s an alternative; hosting your common library on GitHub.

Configuring the package.json

First thing’s first, ensure that you’ve got a repo with your common library source code hosted on GitHub.

Next, add the following to your common library’s package.json:

"scripts": {
"prepare": "yarn run build" // or whatever your build command is

Since we’ll be hosting the source code of your common library on GitHub, when it’s downloaded, it’ll need to be compiled. Your package manager will run the common library’s prepare script when you attempt to install it.

Configuring your local git installation

Note: the instructions for this step are now deprecated. Newer instructions can be found here.

If your common library is hosted in a private GitHub repo, you’ll need to tweak your local git config to allow it to authenticate with GitHub using an access token:

git config --global url."https://$".insteadOf ""

Instructions on creating an access token for your repo can be found here.

Installing the package

  • Tag your common library on GitHub, giving it a version number (e.g. v0.0.1 )
  • Install your common library using yarn install
  • All going well, the library should download, install via the prepare script, and be added to your dependencies list.
Tom Szpytman

Written by

Full-stack Software Engineer.

More From Medium

Also tagged JavaScript

Also tagged Github

Also tagged React

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade