node.jsnpmversionsemantic-versioningnpm-init

"npm init" and using non-semver standards


When creating a new node.js project, the following is run first:

npm init

And there is a series of questions that will help initialize the package.json file. One of those questions is about version.

The version number that is supplied as an answer should strictly follow the semver standard. Now, what if I would like to use a different versioning standard that does not adhere to semver? How to enforce using different version standard? Can I also supply the new rules for the new standard to be used instead of semver to enforce the new versioning standard?

Thanks.


Solution

  • You should never violate the contracts that are inherent in using a particular packaging/distribution tool. In this case, npm require SemVer because it provides a useful communications channel between publishers and consumers that is easily automated. Since your versioning scheme makes no distinction between bug fixes, new features and breaking changes, the best that you can do is apply a mapping from it onto SemVer, if you must use npm as your packaging scheme. There's two ways I can think of to accomplish this.

    1. Use 0.0.1 for all versions and append a -prerelease that contains your actual sequential version. These will sort as you expect them to do provided each of the dotted prerelease fields is pure numeric and they will always be legal in terms of semantic versioning.
    2. User 0.0.X, where X is the most significant field of your versioning scheme and you place the remaining values in the -prerelease tag as pure numeric doted fields.

    Either of these schemes is legal SemVer and announces to all consumers that any new version is potentially a breaking change. The 0.0 prefix in SemVer essentially means all bets are off. This way, consumers willing to accept risk can automatically update to your latest version and those who are unwilling to accept such risks can avoid them.

    Examples based on your comments:

    0.0.1-2018.01
    0.0.1-2018.02
    0.0.1-2019.03
    
    0.0.2018-01
    0.0.2018-02
    0.0.2019-03
    

    After providing examples above, I personally prefer the second variation over the first one, but as far as the SemVer rules are concerned, they are literally equal.