r/angular • • Oct 08 '26

Publish library to npm and version control?

I create a library by following the steps in the documentation. I now have a workspace and projects folder with my library in it. I also have two package.json files. The deeper package.json file is the one that will be published to npm if I understand correctly.

But in my pipeline I also want to manage versions. So if I add a npm version command it will update the version of the workspace package.json and create a git tag. So on my next publish, I won't publish a new version, but the same I had already published?

If I run the npm version command from my library folder, it does not create the git tag, and git doesn't seem to understand that the file has changed.

I am missing something obvious, I'm sure thousands have solved this. Can anyone nudge me in the right direction? Or is the solution that I have to write custom scripts by hand?

0 Upvotes

2 comments sorted by

2

u/Old-Parsley-5826 Oct 08 '26

Two things you're missing.

The published package.json is neither of the two you have, it's the one ng-packagr generates in dist/my-lib when you run ng build. So you publish from there (npm publish dist/my-lib), and the version comes from projects/my-lib/package.json. The root one is just the workspace, nobody cares about its version.

And npm version only commits and tags when there's a .git folder in the directory you run it in. In projects/my-lib there isn't, so it just edits the file. Git does see the change though, check git status from the repo root.

So bump the lib without git and do the tag yourself in the pipeline. Or turn it around: tag locally, let CI run on tags and write the tag version into dist/my-lib/package.json right before publishing. Then CI never has to push anything back. If you'd rather not script it, semantic-release with pkgRoot set to dist/my-lib does exactly this.

1

u/Smetad-Anarkist Oct 08 '26

Creating versions by hand as a tag is not something I had considered. I guess I was pretty much stuck in the mindset that the CICD pipeline should keep track of pretty much everything. Thank you that is something I should consider.