npm update version invalid, npm version patch, npm version minor, npm version major, npm update

Publish and update package cover

1. About publishing and updating packages

Having a problem - npm version patch doesn't have an updated version!

For the first time, a simple package was released on npm. After the release was successful, a new readme.md file was added to describe the project clearly. After executing the update command, the version npm version patchnumber was output, but there was no update when viewing the npm website and executing npm view versions version, what is the situation?

# 第一次发布
$ npm publish
# 输出
...
npm notice Publishing to https://registry.npmjs.org/
+ [email protected]

# 新增 readme.md 文件后
$ npm version patch
# 输出
v0.0.2

# 这时查看版本列表
$ npm view create-captcha versions
# 输出 只有一个版本
0.0.1

When only npm version patch is executed, the online package is not updated successfully.

How to handle the update to take effect?

Continue to check the documentation on the npm official website, and find that after the version update operation is performed, npm publish is also required to release the new version. After the three commands of npm version patch/minor/major are executed, you need to execute npm publish to publish.

# 微小版本更新,对应版本号v0.0.1 就是 v<major>.<minor>.<patch>, v微小更新.次要更新.主要更新
$ npm version patch
$ npm publish

$ npm version minor
$ npm publish

$ npm version major
$ npm publish

View update release results

There are two ways to view the results. One is to open npmjs.com to search for your own package, click to view the version number, and the other is to view it through the terminal command line.

# 列出所有线上版本
$ npm view create-captcha versions
# 输出
[ '0.0.1', '0.0.2' ]

# 仅列出线上最新版本
$ npm view create-captcha version
# 输出
0.0.2

2. About the development project update depends on npm update

After the updated version of the package is released, use npm update to update the project that depends on the package, but it is found that the update is not successful. So check the npm documentation again to find out why.

Different meanings of project dependency version identification

In the package.json of our project, there are three representation methods for the field identifying the dependent version, corresponding to different update rules.
Whether in the dependencies or in the devDependencies field, ^ means that the major version is consistent, and ~ means the minor version

"dependencies": {
    
    
	"pkg1": "^1.2.3",  // 安装大版本为1的latest最新版本
	"pkg2": "~1.2.3", // >=1.2.3 <1.3.0,次要版本不能高,只更新补丁小版本
	"pkg3": "1.2.3",  // 具体版本
	"pkg4": "^0.1.2"  // 大版本低于1.0.0
	"pkg5": "^0.0.1", // 大版本低于1.0.0
	}

pkg1 is marked with ^, and the latest version with the same major version can be installed (>=1.2.3 <2.0.0);
pkg2 is marked with ~, and the highest version with the same minor version can be installed (>=1.2.3 <1.3. 0);
pkg3 has no identifier, only specific fixed version 1.2.3 can be installed;

In particular, when the major version is lower than 1.0.0, the role of ^ is not to limit the major version.
pkg4 uses ^ to identify, and
pkg5 uses ^ to identify. In this case, the version number of package.json can only be carried out first. renew.

Take the React version as an example

We use npm view react versionsto see all versions of react

Take react version pruning as an example
[
'0.0.1', '0.0.2', '0.0.3',
'0.1.2', '
0.2.0', '0.2.1', '0.2.6',
'0.3.0', '0.3.4',
'0.14.0', '0.14.1', '0.14.10',
'15.0.0', '15.0.1',
'18.2.0'
]

Create a new directory, initialize the project, and install the minimum version of react

$ mkdir test-react
$ cd test-react
$ npm init -y
$ npm i [email protected] -S

We get dependency version number in package.json

"dependencies": {
    
    
    "react": "^0.0.1"
}

then try to update

$ npm update react -S
# 输出 up to date, audited 2 packages in 34s

The update command updates the version of react lower than 1.0.0 in package.json, indicating that it is already the latest version. Next, change the version in package.json to 0.0.2, and execute the update command again.

// package.json
"react": "^0.0.2"

// shell
$ npm update react -S
# 输出 changed 1 package, and audited 2 packages in 8s

The update of react has successfully changed to 0.0.2. It can be seen that when the version is lower than 1.0.0, the version identified by the ^ symbol can only be limited to the minor version identified by it.

Change again package.json, change react to a minor version of 2, that is "react": "^0.2.0", perform an update

$ npm update react -S
# 输出 added 3 packages, changed 1 package, and audited 5 packages in 11s
# 此时 node_modules目录有 ensure-array eventemitter2 react sprintf 四个目录

At this time, check package.json dependency has changed to "react": "^0.2.6", updated to the latest version with minor version 2, and the next version is 0.3.0. Paste the version number list again to check, so we know how the ^ symbol works when it is lower than 1.0.0.
[
'0.0.1', '0.0.2', '0.0.3', '
0.1.2',
'0.2.0', '0.2.1', '0.2.6',
'0.3.0', ' 0.3.4',
'0.14.0', '0.14.1', '0.14.10',
'15.0.0', '15.0.1',
'18.2.0'
]

Another way, update by reinstalling latest versionnpm i xxx@latest -S

Going back to the package I released myself, re-executing the installation command can also update the dependencies to the latest version, of course the same is true for react, try it out.

$ npm install create-captcha@latest -S
# 输出
changed 1 package, and audited 2 packages in 4s

How to use version identification correctly?

  1. When we start a new project, it is generally a good choice to install and use the latest dependency packages. After all, the old version will gradually lose maintenance.

  2. But when we are developing in an existing project, we need to ensure that the dependent version is compatible with the project, and it is best to be highly consistent with the version deployed in the production server, so as to avoid the inconsistency between the production and development environments, which makes troubleshooting very difficult.

View version update results

Use npm list to view all dependency versions installed in the current project.

$ npm list

Whether it is developing a release package or using dependencies in a project, we need to have a clear understanding of the version. Doing this step well can avoid environment-related problems in the subsequent development process and improve development efficiency and happiness!

Guess you like

Origin blog.csdn.net/zhouweihua138/article/details/129523382
NPM