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

Asked

Viewed 11,548 times

3

I installed some packages from 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 the composer PHP, when we no longer use a package (that is, we remove it from the list of dependencies of the configuration file json), the same is removed.

You can do it in Npm of NodeJS?

  • I don’t know if it solves your problem, but select the folders of the packages and delete, delete the packages as well.

2 answers

10


In the projects where there is the archive package.json, besides the Uninstall already mentioned in the previous answer, you can use the command:

node prune

Simply edit the file package.json so that it contains only the dependencies that you use and runs the command that it will delete all packages that are in the node_modules and which you do not refer to in package.json.

It is also recommended that package.json you put in the script of postinstall the command npm prune so that after a version update for example, packages not used anymore will be 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 the [email protected], will be removed from node_modules with the command node prune or after a npm install no more parameters.

0

To add the packages npm install <nome_pacote>

To remove npm uninstall <nome_pacote>

  • 1

    But do you have to remove one by one? It does not automatically recognize how Composer does?

  • Well if I’m not mistaken, you can use the command npm prune , he will "rivet" the file package.json and will only leave installed the packages that are described in this file, the rest it removes.

  • Ah, yes. I tried to use this one, also rsrsrs. In the English answer says it works, I will test

  • Why did you deny the boy’s answer ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.