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 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.
if [[ $TYPE =~ ^(patch|minor|major)$ ]]; then
echo "Invalid version type. Use patch, minor or major"
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:
- Avoid yarn to automatically create a tag when creating a new version.
- Clear the existing build and building the package again.
- Create a new version using Yarn. https://yarnpkg.com/lang/en/docs/cli/version/
- Get the new version and store it in
- Push the tag and update master.
And finally, how do I use it:
and of course, you are supposed to change the v1.0.0 depending on the version/tag you want to use in your app.