20

I wanted to try grunt-babel, so I opened up a terminal in my Home folder and did npm install --save-dev grunt-babel babel-preset-es2015 according to the plugin's instructions.

I was doing this too hastily, and realized I should probably have done this in my new project folder where I am dabbling with ES6 code. I had not even done npm init in that folder nor in the Home folder from where I executed the install command.

When I do npm uninstall grunt-babel, the preset files are removed but 91 folders of different dependencies remain in the node_modules folder.

Can I simply remove the folder instead of running npm uninstall 91 times?

This guy asked a similar question but none of the answers address his subquestion of just removing the folder: how to uninstall npm modules in node js?

Community
  • 1
  • 1
pastic
  • 531
  • 2
  • 5
  • 12

3 Answers3

25
  • npm uninstall <name> removes the module from node_modules, but not package.json.

  • npm uninstall <name> --save to also delete the dependency from package.json.

  • npm rm <package_name> removes the packages when uninstall not working

  • npm prune <name> (see docs) for extraneous packages and packages that are not listed on the parent package's dependencies list.

If you don't want to uninstall one by one run
rm -rf node_modules && npm cache clean && npm install
It's a good way for being sure the packages you uninstall are no more in the packages json.

Now in 2021 npm uninstall <name> will also removed it from package.json

EQuimper
  • 5,811
  • 8
  • 29
  • 42
  • 2
    How does that answer the question of manual removing and npm uninstall difference .It's good you added types of removal but can you please give specific answer – Sai Ram Oct 11 '18 at 07:11
  • `npm uninstall ` DOES remove it from package.json in 2021. Can someone update this answer? – JAstuccio Jun 28 '21 at 14:54
20

UPDATED answer (2020):

These are all aliases to uninstall: remove, rm, r, un, unlink

And today there is no need for --save flag since it is the default. The same goes for install BTW.

eyalyoli
  • 1,677
  • 17
  • 16
2

Use npm list as a tool to understand your changes. I usually use the time to make a capture file like:

npm list >1307

do some change

npm list >1309

so then:

cat 13??

or an editor lets me see what npm thinks it did.

For uninstall, only packages on the root all size of 'whole package' get removed. Other then that, the command is politely ignored...

For example:

├── safe-stable-stringify@1.1.0
├── semver@6.3.0
├─┬ tableify@1.1.0
│ └─┬ optimist@0.6.1
│   ├── minimist@0.0.8 deduped
│   └── wordwrap@0.0.3

safe-stable-stringify is a removal candidate, but wordwrap is not. Think about it, this is entirely reasonable !

npm uninstall pkgtoyank -save

updates packages.json by removing it from there as well.

npm is very well designed to say the least. I usually hugely avoid directly poking under it in ./node_modules I will copy things out from there to look at them, but why yank on a leash of a BIG CAT and get bit. it works; use it as its intended....

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Dan Kolis
  • 41
  • 4