npm publish patch for earlier major version

I can't seem to find information about how npm works with branches within a repository.

Suppose an npm package is currently versioned at: 1.0.5

A major change requires a version change from 1.0.5 => 2.0.0

Some users continue using 1.x.x to avoid breaking changes.

If a bug is discovered in 1.0.5 it needs to be fixed for the 1.x.x users requiring version change from 1.0.5 => 1.0.6

In effect, this is branching. I'd make a git branch for 1.x.x users and continue using git's master branch for 2.x.x

But how does this fit in with npm? Should I publish an older npm version 1.0.6? In that case doesn't 1.0.6 become the latest while actually 2.0.0 should be the default when doing npm install.

I can't find branch related information for npm. I'm sure the above is a common situation but I just can't find any info. Please can someone point me in the right direction.


Solution 1:

You are on the right track - you want to publish [email protected] without updating the latest tag. You can do this by supplying a --tag <tagname> argument to npm publish --

cd project
git checkout old-branch
grep version package.json
  "version": "1.0.5",
[make changes]
git commit
npm version patch
grep version package.json
  "version": "1.0.6",
npm publish --tag old-version

As long as you supply a --tag <tagname> argument to npm publish, the latest tag will not be updated, and people using npm install <package> or npm install <package>@latest will still get the 2.x version.

Note that the tagname has to share a namespace with version numbers, so it's best to choose a tagname that doesn't look like a semver version; avoid '1.0.6' or 'v1.0.6'.

Source: https://docs.npmjs.com/cli/publish and: https://docs.npmjs.com/getting-started/using-tags