Update the version number using the NPM command line

Time:2021-10-27

background

Version number is a set of public rules for the gradual evolution of software and between its users. Semantic versioning semantic version number is a set of public conventions on how to specify the version number. In our daily software production process, we not only produce the software itself, but also produce some reusable code packages. These code packages are integrated to produce a complete software and system. During this period, the code package will continue to develop, add new features or fix old problems. Under this trend, software production and iteration can be better refined based on semantic version number rules, so that software assets can be updated and released orderly.

The version number basically consists of three digits:
   1   .   0   .   0
[MAJOR].[MINOR].[PATCH]

The three digits represent different meanings:

  • Version of major when making incompatible API changes
  • Version of minor when adding features in a backward compatible manner
  • Patch backward compatible bug fix version

I suggest you check the detailsSemantic versioning semantic version numberconvention.

problem

The team delivered some manual operations to CI / CD tools, which is more than the release version of the software discussed in this article. At present, the software release process is based on manually modifying the version field in the package.json file to update the version. After the front-end begins to manage various public modules in a modular manner, a large number of code packages will be produced and need to be maintained. Some artificial operations are delivered to the machine to help front-end colleagues solve some repetitive work, which can also effectively reduce human errors in these links.

terms of settlement

NPM, the current code package management tool in the front end, provides command tools to help coders solve version upgrade. Here is the code:

$ npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]

'npm [-v | --version]' to print npm version
'npm view <pkg> version' to view a package's published version
'npm ls' to inspect current package/dependency versions

The rules used are also designed in accordance with the semantic version number convention. See sermver.inc for details of the official practice of NPM.

The following is a description of the version options (assuming the default version is 0.2.0):

option describe example explain
major Major updates npm version major 0.2.0 =》1.0.0
minor Major updates npm version minor 0.2.0 =》0.3.0
patch Patch update npm version patch 0.2.0 =》0.2.1
premajor Major update pre release version npm version premajor 0.2.0 =》1.0.0-0
preminor Mainly update the pre release version npm version preminor 0.2.0 =》0.3.0-0
prepatch Patch update pre release version npm version prepatch 0.2.0 =》0.2.1-0
prerelease Pre release version npm version prerelease If the current version is not a pre release version, an error will occur
from-git Take git’s tag as the version number and set it in package.json npm version from-git If the tag tag of Git is not set, an error will be thrown

The use of prerelease is the premise of existence. The current version must be a pre release version. If it is not a pre release version, an error will be thrown. You should pay attention to it when using it. Using [– preid] to provide additional parameters can also be used to describe the role of the pre release version in detail. For example, the following needs to be implemented:

#Update to pre release version number
$ npm version premajor --preid beta
v1.0.0-beta.0

Reference to:https://docs.npmjs.com/cli/v7…

Use examples

Scenario 1: simple use

Current version: 0.2.0

Behavior: update the version to 0.2.1 and tag v0.2.1 in GIT.

#Update to the latest patch version
$ npm version patch

#Print git history
$ git log
commit 39c8ba50f0ef18aab41ac9c65669b2769ed3b3a7 (HEAD -> master, tag: v0.2.1)
Author: Packy <[email protected]>
Date:   Wed Apr 7 16:18:22 2021 +0800

    0.2.1

Scenario 2: Customize git submission content

Current version: 0.2.0

Behavior: update the version number and submit git, and customize the submission description.

$NPM version major - M "version updated to% s"
v1.0.0

$ git log
commit 7b9e3102111f8f86fc70d1b1fcab96bb9389df9b (HEAD -> master, tag: v1.0.0)
Author: Packy <[email protected]>
Date:   Wed Apr 7 17:01:36 2021 +0800

    Version updated to 1.0.0

commit eda4316722a9d03f2fd5e60f61507a6e272ddc1b
Author: Packy <[email protected]>
Date:   Wed Apr 7 11:01:20 2021 +0800

    Chore: update package

Scenario 3: no git operation

Current version: 0.2.0

Behavior: only change the version field in the package.json file without git operation

#Update to the new patch version without adding git's tag
$ npm version patch --no-git-tag-version

$ git log
commit eda4316722a9d03f2fd5e60f61507a6e272ddc1b (HEAD -> master)
Author: Packy <[email protected]>
Date:   Wed Apr 7 11:01:20 2021 +0800

    Chore: update package

$ git status
On branch master
Your branch is ahead of 'origin/master' by 6 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   package.json

no changes added to commit (use "git add" and/or "git commit -a")

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]