About the method and problem solving of version locking in package.json

Pre-knowledge: first understand the relationship and difference between package.json and package-lock.json, please read this article

Then let's talk about how to lock the version?

First of all, you must remove the ^ symbol in package.json, but if you only remove ^ in package.json, it would be too naive, there must be a big pit waiting for you, because we actually use package-lock Version in .json. [Yes, I just came out of the big pit. .

Before the version was locked, our project looked like this

If you only remove the ^ in package.json without modifying the package-lock.json file, then generally there will be no problem, because other environments are also installed according to package-lock.json. However, there are several situations where problems can arise

(1) If you use cnpm, the problem will be big, because cnpm does not have the function of version locking, cnpm will not install according to package-lock.json, so it will install version 4.13.0, which does not match what we actually use, which is very important Something could go wrong.

(2) When the installation fails, we are sometimes used to delete the package-lock.json file and then install it again. This is a tragedy. After the reinstallation, the package-lock.json is also version 4.13.0. Then if you submit this package-lock.json again, all colleagues will install a wrong version.

Therefore, we should update package.json according to the version actually applied by package-lock.json when we lock the version . In the above example, change node-sass to version 4.14.1 in package.json and remove the preceding ^. This basically won't be a problem anymore.

But there is another problem. The version locked according to the above method means that the dependent version of our project is locked, but the indirectly dependent version is not locked. For reference, please lock your npm dependency version - Nuggets

However, in the projects I encountered, it is enough to set according to the method of my article, because generally there will be no problems, and it is simple and convenient, and you can choose by yourself.

Guess you like

Origin blog.csdn.net/qq_17335549/article/details/130343653