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 .

npm uninstall and rm Directly delete the difference between more related articles

  1. rm( Delete files of the operating system ) And git rm The difference between

    git rm:1. Deleted a file 2. Put the deleted file into the temporary storage area if you want to recover this file , We need to do 2 Operations a.git reset HEAD file_name -- Restore files from staging area to workspace b.git ch ...

  2. npm install、npm init、npm update、npm uninstall and package.json

    npm install Install local package npm install <package_name>: This command will create in the current directory node_modules Catalog ( If it doesn't already exist ), And download the software package to the directory . ...

  3. npm Commonly used instructions Use the instruction to delete node_modules package

    see npm command npm help Global command parameters -g npm install -g Install Global npm uninstall -g Unload the global overall situation node In bag i5ting_toc This bag can handle md writing ...

  4. rm: Cannot delete &amp;quot;/run/user/root/gvfs&amp;quot;: Is a directory problem

    2013-03-02    [email protected]:~$ sudo su [sudo] password for bxd:  [email protected]:/home/bxd# exit exit rm: Cannot delete ...

  5. Linux in RM Quickly delete a large number of files / Folder method

    There was a problem yesterday , stay Linux There's a folder in which there's a lot of Cache file ( clip ), The order of magnitude could be in the millions , Use rm -rf ./* Delete time is too slow to accept .Google For a moment , We found a way , Try it out. It's better than ...

  6. Linux Pass through rm -f Prompt when deleting a large number of files &quot;-bash: /bin/rm: Argument list too long&quot; Solutions for

    Linux Pass through rm -f Delete /var/spool/postfix/maildrop/ Prompt when there are a large number of small files in : "-bash: /bin/rm: Argument list too long& ...

  7. rm: Cannot delete &quot;xxxxx.o&quot; : Input / Output error .

    rm: Cannot delete "xxxxx.o" : Input / Output error . Encountered a file that cannot be deleted , Thought it was over , To be refitted . Just restart later

  8. Read a text npm、yarn、pnpm The difference between

    The author compares the current mainstream package management tools npm.yarn.pnpm The difference between , And put forward the appropriate use suggestions , The following is the translation : NPM npm yes Node.js One of the main reasons for being so successful .npm The team did a lot of work , In order to ensure that n ...

  9. Linux Pass through rm -f Error in deleting a large number of files :Argument list too long

    Linux Pass through rm -f The following error message appears when deleting a large number of small files :  -bash: /bin/rm: Argument list too long As shown in the figure below : Question why If the file to be deleted contains small ...

  10. [ turn ] Read a text npm、yarn、pnpm The difference between

    [From] http://geek.csdn.net/news/detail/197339 original text :Understanding differences between npm, yarn and pn ...

Random recommendation

  1. oracle What is the temporary table used for

    oracle What is the temporary table used for In some cases , need When multiple very large tables are associated , But what needs to be retrieved , It's when there's a small amount of data . You can put it first. Big table data , Retrieve that little part , Then insert into In the temporary table , Finally, close it again ...

  2. html5, A comprehensive case of the form

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  3. JSP Database connection class MySql database

    The main function of the database connection class is to connect to the database and obtain the connection object , And closing the database . By creating an instance of this class , Call one of the methods , To avoid repeated operations . package chapter13; import java.sql.*; p ...

  4. WebRTC Learning from ( Two )

    The link address of the original English text is :https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Overview WebRTC It's made up of some connections API And agreement one ...

  5. SQL Medium delete and TRUNCATE Usage of

    TRUNCATE TABLE Table name Delete all rows in the table , Instead of recording individual row deletions . grammar TRUNCATE TABLE name Parameters name Is the name of the table to truncate or the table whose rows are to be deleted . notes TR ...

  6. Homebrew 1.0.0 Release ,MacOS Package manager on

    Artifact , I don't know how to use it without it macos https://www.oschina.net/news/77367/homebrew-1-0-0

  7. hdu 4715 Difference Between Primes 2013 year ICPC Warm up game A topic Prime water question

    The question : Give an even number ( No matter positive or negative ), Find two prime numbers a,b, Can satisfy  a-b=x, Prime in 1e6 within . Just use the screening method to print out the prime table , Just enumerate and query . I use set Store primes , Then traverse set The elements inside , Inquire about +x Whether to return after ...

  8. Sql Server Art ( One ) Add, delete, check and modify views

    Views are another way to query data from one or more tables . With views, you can focus . Simplify custom database , At the same time, it's safe . The structure and data of the view are based on the corresponding query . Like a watch , Views also include several defined data columns and multiple data rows , but ...

  9. How to judge whether a page is qq Browser or wechat browser

    // Judgment is QQ Browser or wechat browser js Code isWx = function() { var ua = navigator.userAgent.toLowerCase(); return ua.mat ...

  10. Nginx Use the tutorial ( Four ): Improve Nginx Network throughput buffers Optimize

    The request buffer is NGINX It plays an important role in request processing . On receipt of the request ,NGINX Write it to these buffers . The data in these buffers can be used as NGINX Variable usage , for example $request_body. If the buffer is compared to the requested size ...