24

I think I've been using npm incorrectly, most of the time installing globally (with -g) instead of locally (with -save). Now I have a long list of global packages that I want/ought** to clean up.

(**I'm new to this, and in addition to being OCD-tidy, I don't want to accidentally use a package in an app without explicitly mentioning it, otherwise it might become a tough puzzle to deploy someplace.).

I understand that I can use npm uninstall <package> -g to do the actual removal, but how do I decide what should stay? There's a looong list of things named like "cuddly-bunny@1.2.3" and so on.

Is there a way to determine if a project is using it, or if its the kind of thing I need globally? It will be a long day going to the docs for each one. Or maybe there's a rule of thumb, like: "one typically needs x, y, and z, and you can delete the rest"?

Also, once I have a removal list, is my only choice to execute all of those unistall -g commands one by one?

Prasad
  • 1,562
  • 5
  • 26
  • 40
user1272965
  • 2,814
  • 8
  • 29
  • 49
  • 1
    Generally, if you run it from the command line, it's a global package, whereas if you `require()` it in a project, it should be local. – Joe Clay Mar 07 '16 at 15:43
  • I just learned that most of the packages are installed as parts of others, and (I think) will get removed when I remove the parent. The list isn't as long as I thought, and I recognize most of the names on the left side of the list. I guess I should delete this question, but stackoverflow says I can't because it has an answer. – user1272965 Mar 07 '16 at 16:01
  • 1
    It was a valid question IMO - no harm in leaving it up. – Joe Clay Mar 07 '16 at 16:04
  • Why do you think installing globally is the wrong way? (I too am a new user, and I too avoid installing locally, but is this wrong?) – Ayyash Mar 07 '16 at 18:16
  • @Ayyash - after I finish writing my app, I want to host it someplace. I haven't figured this part out at all, but I'm pretty sure that the global packages won't be on the hosted machine. – user1272965 Mar 07 '16 at 18:38
  • Ember has more than 15,000 files, i doubt it is meant to be hosted, i think most of these files are important during development only (like creating an adhoc server), that should not be needed in production – Ayyash Mar 07 '16 at 18:43
  • 1
    @Ayyash - If you globally install a package and then use it in two projects, you can't update it for one and not the other, whereas if you install it locally to a project, you can have a separate version in each project without them interfering. Also, if you have your packages locally installed, and saved to your `package.json`, it means you don't have to copy `node_modules` if you want to develop on another computer; you just have to run `npm install`, and it'll pull them all in automatically. Locally installing packages just generally makes tracking/managing a project's dependencies easier. – Joe Clay Mar 08 '16 at 10:28

2 Answers2

30

You can use: npm uninstall -g package_to_uninstall

See:https://docs.npmjs.com/cli/uninstall If you prefer video: https://docs.npmjs.com/getting-started/uninstalling-global-packages

If you want to remove all packages; There is trick: Please follow below link: Command to remove all npm modules globally?

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
silwalprabin
  • 637
  • 7
  • 20
0

this didn't work on ubuntu for pm2, I had to run npm remove pm2 -g

Hom Bahrani
  • 3,022
  • 27
  • 25
  • `npm remove` is an alias of `npm uninstall`, and `rm`, `r`, `un` and `unlink`, Check `npm help uninstall` for more info. – Pablo Bianchi Jul 05 '19 at 18:04