Using your own repos as NPM dependencies

While building my app, I needed a way to build my own common packages and list them as dependencies in my package.json. Of course, you could paid for NPM private packages, but if you’re a small startup trying to save every penny, maybe this is a good approach for you too.

The common packages have a src/ folder where I put the code. They also have a build/ folder where I put the babel transpiled version of my package.

What’s the trick?

Use semver and create a git tag every time you release a new version of your package.

By doing this, you can specify that exact version you just tagged in the package.json as a dependency. Check the NPM official docs: Git URLs as Dependencies.

In the package.json you should specify the following sections for it to work:

"main": "build/index.js",
"scripts": {
  "publish": "./scripts/publish.sh"
}

main indicates that’s the file Node will look for when importing your package.

How does the publish.sh file look like?

To “publish” a new version of my packages I just need to type:npm run publish [patch-type] where patch-type could patch, minor or major.

#!/bin/bash
TYPE="$1"
if [[ $TYPE =~ ^(patch|minor|major)$ ]]; then
  TYPE="$TYPE"
else
  echo "Invalid version type. Use patch, minor or major"
  exit
fi
yarn config set version-git-tag false
rm -rf build
./node_modules/@babel/cli/bin/babel.js -d ./build ./lib --presets=@babel/env
yarn version --new-version $TYPE
version=$(node -p "require('./package.json').version")
git add --all
git commit -m "BUILD - v$version"
git tag v$version
git push origin v$version
git push origin master

Basically, this is what happens in the script:

  1. Avoid yarn to automatically create a tag when creating a new version.
  2. Clear the existing build and building the package again.
  3. Create a new version using Yarn. https://yarnpkg.com/lang/en/docs/cli/version/
  4. Get the new version and store it in version .
  5. Push the tag and update master.

And finally, how do I use it:

"dependencies": {
  "my-private-package": "git+ssh://git@bitbucket.org:[bitbucket-user-name]/[repo-name].git#v1.0.0"
}

and of course, you are supposed to change the v1.0.0 depending on the version/tag you want to use in your app.

Like what you read? Give David Ballesteros a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.