Are there npm version prerelease identifiers?
Asked Answered
T

3

59

There is a very handy npm version command. Besides arguments like major, minor and patch it accepts arguments like prerelease, prepatch, etc.
It says in the docs that the commands work in accordance with the semver.inc function.

These pre commands I have a question about.

Say I'm currently at version v1.0.0.
If I run npm version prerelease it will bump version to v1.0.1-0.

Is it possible to provide an extra agrument for a prerelease identifier according to https://github.com/npm/node-semver#prerelease-identifiers?

I wish something like npm version prerelease alpha would bump version to v1.0.1-alpha.0 but that doesn't work.

Therianthropic answered 15/6, 2017 at 12:3 Comment(0)
T
126

Starting with npm 6.4.0 you can use the --preid option of npm version like this:

$ npm version prerelease --preid=alpha
v0.1.1-alpha.0
$ npm version prerelease --preid=alpha
v0.1.1-alpha.1
$ npm version prerelease --preid=alpha
v0.1.1-alpha.2
Tetrahedron answered 17/8, 2018 at 4:35 Comment(1)
it worth to mention preminor and premajor alsoEpsomite
K
9

Like the other answer mentioned this is not supported by npm because of the reason mentioned in this comment

But you can achieve the same using semver package and npm scripts by adding something like the following to the package.json

"scripts": {
  "beta-version-patch": "npm version $(semver $npm_package_version -i prerelease --preid beta)",
  "beta-version-minor": "npm version $(semver $npm_package_version -i preminor --preid beta)",
  "beta-version-major": "npm version $(semver $npm_package_version -i premajor --preid beta)",
  "rc-version": "npm version $(semver $npm_package_version -i prerelease --preid rc)",
  "final-release": "npm version $(semver $npm_package_version -i)"
}

and run npm run beta-version-patch

To be more generic you can use the following:

"scripts": {
  "semver": "npm version $(semver $npm_package_version -i $release --preid $preid)"
}

and run commands like:

release=prerelease preid=alpha npm run semver
release=prerelease preid=beta npm run semver
release=premajor preid=alpha npm run semver
Kubetz answered 1/8, 2018 at 9:3 Comment(0)
W
6

I have been looking at this recently to see if there were any updates on the matter... but it seems things are still the same.

No, there are no npm version prerelease identifiers supported by the npm version command. You can see the reasoning by the team here: https://github.com/npm/npm/pull/12936#issuecomment-248153743

semver (https://www.npmjs.com/package/semver) does support what you are trying to do, so what you can do is obtain the version with a command just like this:

semver <current version> -i prerelease --preid <prelease identifier>

for example:

semver 1.0.1 -i prerelease --preid alpha

will produce:

1.0.2-alpha.0

With that result you can pass it to npm version (say for example in a CI build), like this:

npm version <resulting version from semver command>

Another alternative is to use semantic-release (an independent project): https://github.com/semantic-release/semantic-release

That will automate semantic versioning based on commit messages, but I think it only works with github repo hosted modules, not sure about that.

Wrac answered 6/2, 2018 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.