How to remove unused packages in the node_modules folder with the NPM command?

0

I installed some packages of NodeJS via npm install . But then I decided to remove some. Even when I install again, the folders of the packages I'm not using remain there.

In% PHP%, when we no longer use a package (that is, we remove it from the dependency list of the composer configuration file), it is removed.

Can you do this in json of Npm ?

    
asked by anonymous 14.03.2016 / 15:27

2 answers

1

In projects where there is the package.json file, in addition to the uninstall already mentioned in the previous response, you can use the command:

node prune

Simply edit the file package.json so that it contains only the dependencies that you use and run the command it will delete all packages that are in node_modules and that you do not reference in package.json . >

It is also recommended that in package.json you put in the postinstall script the command npm prune so that after a version update for example, the most unused packages are removed. >

Example of package.json with script of postinstall :

 {
   "name": "teste",
   "version": "1.0.0",
   "description": "Teste",
   "main": "index.js",
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
     "postinstall": "npm prune"
   },
   "author": "bigous",
   "license": "ISC",
   "dependencies": {
     "luaparse": "^0.2.1"
   }
 }

In this example, any package except [email protected] will be removed from node_modules with the command node prune or after a npm install with no more parameters.

    
04.10.2016 / 18:41
-1

To add the packages npm install <nome_pacote>

To remove npm uninstall <nome_pacote>

    
14.03.2016 / 15:34