当前位置:网站首页>The difference between NPM uninstall and RM direct deletion

The difference between NPM uninstall and RM direct deletion

2022-07-07 21:07:00 Jiahe Bruce Lee

Conclusion :

  • 1. npm uninstall It will back up the package itself node_modules,rm -f Will delete the entire directory
  • 2. npm uninstall The dependent packages will not be deleted . Even if you explicitly want to delete this package , But it is dependent and will not be deleted .rm -f Meeting
  • 3. npm uninstall There is a global mode , Also delete the soft chain ,rm -r Delete directory only , The soft chain may be missed
  • 4. npm uninstall Will delete package.json Medium dependencies entry ,rm -r Can't
  • 5. npm uninstall Will install those in package.json Missing package ,rm -r Of course, there can be no such side effect

All in all ,npm uninstall and npm install It's the same set of code , The same logic , All are 【 Synchronize the current package environment 】 The operation of . and rm -r Is to forcibly delete a directory .

In terms of name ,uninstall It's really not well named , It's very complicated , and install As complex as , As a whole npm The core of design , Easily misunderstood as deletion .

No documents , Only look at the code , Ah

analysis :

such as , To delete mkdirp

npm uninstall mkdir 

 

Will perform the following operations

First step Load the local actual package list

 
readLocalPackageData ( If you use -g The global mode will execute  readGlobalPackageData, This line does not show )
read-package-tree
  This is a bag , This package will read recursively  node_modules Bag under , Return to a list of heavy objects .
 
 
 

 

The second step load package.json Get a list of ideal packages

 Through analysis package.json Get the list 

loadAllDepsIntoIdealTree


 

 stay IdealTree Filter out the package to be deleted , So you get two Tree, One is  CurrentTree, One is  IdealTree

 

The third step   Sync CurrentTree and IdealTree

  • 1.  If CurrentTree in ,IdealTree Does not exist in the , Ignore
  • 2.  If CurrentTree Does not exist in the ,IdealTree in , Install
  • 2.  If CurrentTree in ,IdealTree Does not exist in the , Delete

 

 

Step four The logic of deleting

  • 1. If the package to be deleted is currently IdealTree Other packages in depend on , Do not delete
  • 2. If there are dependent packages in the package to be deleted , Then back them up , Recover later
mv ./node_modules/mkdirp/node_modules ./node_modules/.mkdirp.MODULES
rm -rf
./node_modules/mkdirp/
mkdir
./node_modules/mkdirp
mv ./node_modules/.mkdirp.MODULES ./node_modules/mkdirp/node_modules

 

 Use  rimraf Delete directory ,rimraf It's a bag , It offers something like rm -rf The function of , Recursively delete directories and files , There is retry logic .

It seems simple rm -rf Why should we implement it alone ?

nodejs fs The module was not provided in the early stage rm -r The function of ,fs The module only provides the ability to delete a single directory and a single file .

stay  v12.10.0 After that, it provides the method of recursively deleting directories api, stay  v14.14.0 Provides rm -rf Of api.

It's too late , So in development npm When , We used rimraf This package .

 

If you want to delete the package , No, node_modules, The backup operation will not be performed .

 

 

 

原网站

版权声明
本文为[Jiahe Bruce Lee]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071838363845.html