Releasing packages in Monorepos using NPM or PNPM
How to make pnpm version work in monorepos by creating a temporary .git directory in the package folder.
I was setting up a deployment/release pipeline and ran into an interesting situation that might help if you're working with monorepos.
Long story short, if you didn't know, there's a script called pnpm version
or npm version
that handles the release process for you. What it does is:
- Creates a release commit (you can configure the message)
- Creates a tag with a
v
prefix (for example,v1.0.1
) - Bumps the version in
package.json
However, this doesn't work in monorepos by default. You need to apply some workarounds to make it function.
Let's say you have an app in apps/web
and you want to release each app/package individually, so each package can have its own version. By default, this won't work because pnpm version
(and npm as well) expects you to run the command from the root directory. If you try to run it from any other directory, it won't create a commit or a tag—only the package.json
version is updated.
That's because the script looks for a .git
directory. If it doesn't find one, it won't do any of those extra steps. Here's a quick hack you can use:
This will create a .git directory in the correct package (it always creates the directory, and if it exists, it will override) and then run pnpm version in that package’s context.
Published on January 14, 2025 • 2 min read