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
"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://$GITHUB_ACCESS_TOKEN:@github.com/".insteadOf "https://github.com/"
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.
- Install your common library using
yarn install https://github.com/my-name/my-common-library#v0.0.1
- All going well, the library should download, install via the prepare script, and be added to your dependencies list.