463

Is there a command to remove all global npm modules? If not, what do you suggest?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
EhevuTov
  • 20,205
  • 16
  • 66
  • 71

30 Answers30

598

The following command removes all global npm modules. Note: this does not work on Windows. For a working Windows version, see Ollie Bennett's Answer.

npm ls -gp --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -g rm

Here is how it works:

  • npm ls -gp --depth=0 lists all global top level modules (see the cli documentation for ls)
  • awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' prints all modules that are not actually npm itself (does not end with /npm)
  • xargs npm -g rm removes all modules globally that come over the previous pipe
Community
  • 1
  • 1
Kai Sternad
  • 22,214
  • 7
  • 47
  • 42
  • Almost, need it on the `npm ls` command as well. I tried to fix it myself but edits need to be at least 6 characters... – Bill Feb 14 '12 at 20:33
  • Will this command work under Ubuntu 12.04? I tried running the command as root user, and it still didn't work properly. – Anderson Green Oct 01 '12 at 19:31
  • 6
    Not to be the awk golf guy, this can be done in a single awk command without grep: `awk -F' |@' '/@/ {if ($(NF-1) != "npm") {print $(NF-1)}}'` Explanation: split on spaces or @, only match lines with @, the module name will be the second to last match (`$(NF-1)`), so only print if it's not `npm` – Fotios Jul 11 '13 at 18:14
  • I had to change the last `xargs npm -g rm` to `xargs npm -g uninstall` to get it to work on my machine – SyntaxRules Aug 08 '13 at 19:34
  • It didn't work on my Ubuntu system either, the tree was not removed (don't know why...). I tried a sed regex replacement too, and failed, but perl worked: npm -g ls | grep -v 'npm@' | perl -pe 's/[^[:ascii:]]//g;' | awk -F@ '{print $1}' | grep -v "/usr/local" | xargs sudo npm -g rm (/usr/local appeared as the first line, had to remove it...) – Rohit Chatterjee Oct 12 '13 at 12:00
  • Preview what this will remove by replacing ```xargs npm -g rm``` with ```xargs -L1 echo```. I think the ```npm ls -gp``` at the beginning should be ```npm -ls -p```. – fisherwebdev Dec 27 '13 at 09:18
  • @fisherwebdev `npm ls -p` (ls not prefixed by dash) is correct – Kai Sternad Dec 27 '13 at 10:56
  • 31
    Warning: the new version doesn't filter out the npm module. You don't want to remove that one. – neverfox Mar 20 '14 at 06:06
  • 11
    I also ran the new version without reading the comments. ALWAYS READ THE COMMENTS. Here is how to restore NPM: curl https://www.npmjs.org/install.sh | sh – Jack Allan Jul 08 '14 at 22:12
  • Recently the npm installer was removed, you need to reinstall node instead. On a Mac, with brew you can just do `brew update; brew upgrade node`. – Emil Stenström Jun 09 '15 at 09:04
  • `curl -L https://www.npmjs.com/install.sh | sh` - I wasn't paying attention and missed the missing https:// when I copied from @Jack's answer. Also - https://gist.github.com/DanHerbert/9520689 (not me) is worth a note. – Dan Jun 21 '15 at 23:26
  • 16
    Wow. NPM doesn't make it easy to uninstall things. With bundler and gems, it's trivial to uninstall everything, the fact that you have to use grep and such is a horrible design. Is there a simpler way to do this? Who actually remembers the code required in the answer? – Brian Dear Aug 31 '15 at 11:23
  • Might as well blow away your global `npm` and consider reinstalling it through `nvm install node`, see https://github.com/creationix/nvm – lionello Feb 16 '16 at 03:07
  • OMG, pipes, awk! This will not work from win cmd from-the-box. – Dzmitry Prakapenka Apr 12 '16 at 10:07
  • 16
    @neverfox and others: Fixed, npm itself is no longer removed. Sorry for the inconvenience – Kai Sternad May 04 '16 at 16:42
  • @KaiSternad I would double check? it seems to still have removed it on CentOS 7 – Félix Adriyel Gagnon-Grenier Oct 05 '16 at 14:34
  • 2
    This does not remove modules that have a / in the name, e.g. @angular/cli. – Sergei Krupenin Apr 14 '20 at 23:59
  • try the node cli library i wrote few months back, which will help in removing the all node_modules in the system. https://github.com/uttesh/mo – Uttesh Kumar Aug 21 '20 at 11:40
332

For those using Windows, the easiest way to remove all globally installed npm packages is to delete the contents of:

C:\Users\username\AppData\Roaming\npm

You can get there quickly by typing %appdata%/npm in either the explorer, run prompt, or from the start menu.

Community
  • 1
  • 1
Ollie Bennett
  • 4,424
  • 1
  • 19
  • 26
185

I tried Kai Sternad's solution but it seemed imperfect to me. There was a lot of special symbols left after the last awk from the deps tree itself.

So, I came up with my own modification of Kai Sternad's solution (with a little help from cashmere's idea):

npm ls -gp --depth=0 | awk -F/node_modules/ '{print $2}' | grep -vE '^(npm|)$' | xargs -r npm -g rm

npm ls -gp --depth=0 lists all globally-installed npm modules in parsable format:

/home/leonid/local/lib
/home/leonid/local/lib/node_modules/bower
/home/leonid/local/lib/node_modules/coffee-script
...

awk -F/node_modules/ '{print $2}' extracts module names from paths, forming the list of all globally-installed modules.

grep -vE '^(npm|)$' removes npm itself and blank lines.

xargs -r npm -g rm calls npm -g rm for each module in the list.

Like Kai Sternad's solution, it'll only work under *nix.

Community
  • 1
  • 1
Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
  • 1
    Where are these files stored, I hate this method. Isn't there just a global package.json somewhere? – Evan Carroll Apr 07 '14 at 19:24
  • 2
    @EvanCarroll Nope, there is no such file, but `npm` installs all its global modules to the same directory. The exact location may vary, but typically it's `/usr/local/lib/node_modules`. – Leonid Beschastny Apr 07 '14 at 20:16
  • 3
    Just FYI, This one also removes npm – BrDaHa Nov 04 '14 at 17:41
  • @BrDaHa hm, thanx, I'll check it. But it's strange since I'm explicitly filtering `npm`. – Leonid Beschastny Nov 04 '14 at 18:12
  • @BrDaHa just checked it on my ubuntu with npm `1.4.21` and it worked fine. Though, it attempted to remove `n` node binary manager and `node-gyp` addon builder, but not `npm`. – Leonid Beschastny Nov 04 '14 at 18:42
  • I was using this command on OSX Mavericks. Nbd, I was reinstalling all of node anyway :) – BrDaHa Nov 04 '14 at 19:18
  • This command will return an error if there are no modules other than npm installed (it does not return code 0 for a 'perfect' pre-existing condition). – a2f0 Jun 11 '15 at 20:59
  • 1
    @dps thanx! Now it should work fine even if there are no modules other than npm installed. – Leonid Beschastny Jun 12 '15 at 14:57
  • 11
    This command works on OSX and doesn't remove npm `npm ls -gp --depth=0 | awk -F/node_modules/ '{print $2}' | grep -vE '^(npm)$' | xargs npm -g rm` – real_ate Jul 03 '15 at 15:55
  • grep wasn't working complaining about empty sub patterns. Also npm errors were not being filtered out. This fixes that (using OS X): `npm ls -gp --depth=0 --silent | awk -F/node_modules/ '{print $2}' | sed '/^$/d; /^npm$/d;' | xargs npm -g rm` -- decided to use sed instead – Luke Apr 21 '16 at 18:20
  • @LeonidBeschastny I changed and corrected my answer, would you please change yours accordingly ? Thanks. – Kai Sternad Jun 24 '16 at 07:11
  • @KaiSternad I've tried both our solutions with several `node`/`npm` versions. `npm` module was efficiently filtered out every time. I also failed to see any difference from your last edit. I guess people comply about `npm` being removed because `npm@3` flattens dependency tree, so both our solutions may remove some of it's dependencies, thus breaking it. – Leonid Beschastny Jun 26 '16 at 12:15
  • 4
    This command failed to handle scoped package (like `@angular/cli`). I add another matcher for `awk` and the working command for me looks like this: `npm ls -gp --depth=0 | awk -F/ '/node_modules\/@/ {print $(NF-1)"/"$NF} /node_modules\/[^@]/ && !/\/npm$/ {print $NF}' | xargs npm -g rm` – Jack Q Jul 17 '17 at 17:32
81
sudo npm list -g --depth=0. | awk -F ' ' '{print $2}' | awk -F '@' '{print $1}'  | sudo xargs npm remove -g

worked for me

  • sudo npm list -g --depth=0. lists all top level installed
  • awk -F ' ' '{print $2}' gets rid of ├──
  • awk -F '@' '{print $1}' gets the part before '@'
  • sudo xargs npm remove -g removes the package globally
cashmere
  • 2,811
  • 1
  • 23
  • 32
  • This version worked best for me as of June '14. The only addition could be to filter out "UNMET" dependencies from the list, but that's not critical, as `npm remove UNMET` simply does NOOP. – kangax Jun 08 '14 at 15:30
  • 7
    would add `grep -v npm` so that npm itself don't get removed: `sudo npm list -g --depth=0. | grep -v npm | awk -F ' ' '{print $2}' | awk -F '@' '{print $1}' | sudo xargs npm remove -g` – brauliobo Mar 22 '15 at 21:30
  • there it goes, your npm! – All Іѕ Vаиітy Apr 22 '17 at 09:12
  • Even here in 2023 on a new M2 this helped me sort out some weird problems trying to build a project. – Stuart Jun 08 '23 at 10:54
32

For those using Powershell:

npm -gp ls --depth=0 | ForEach-Object { Get-Item $_ } | Where { $_.Name -ne 'npm' } | ForEach-Object { npm rm -g $_.Name }

To clear the cache:

npm cache clear
Kedar Vaidya
  • 678
  • 1
  • 7
  • 14
26

Just switch into your %appdata%/npm directory and run the following...

for package in `ls node_modules`; do npm uninstall $package; done;

EDIT: This command breaks with npm 3.3.6 (Node 5.0). I'm now using the following Bash command, which I've mapped to npm_uninstall_all in my .bashrc file:

npm uninstall `ls -1 node_modules | tr '/\n' ' '`

Added bonus? it's way faster!

https://github.com/npm/npm/issues/10187

How do you uninstall all dependencies listed in package.json (NPM)?

Community
  • 1
  • 1
jedmao
  • 10,224
  • 11
  • 59
  • 65
22

If you would like to remove all the packages that you have installed, you can use the npm -g ls command to find them, and then npm -g rm to remove them.

Bill
  • 25,119
  • 8
  • 94
  • 125
19

in windows go to "C:\Users{username}\AppData\Roaming" directory and manually remove npm folder

17

If you have jq installed, you can go even without grep/awk/sed:

npm ls -g --json --depth=0 |
  jq -r '.dependencies|keys-["npm"]|join("\n")' |
  xargs npm rm -g

On Debian and derived you can install jq with:

sudo apt-get install jq
eush77
  • 3,870
  • 1
  • 23
  • 30
5

OS not specified by OP. For Windows, this script can be used to nuke the local and the user's global modules and cache.

I noticed on linux that the global root is truly global to the system instead of the given user. So deleting the global root might not be a good idea for a shared system. That aside, I can port the script to bash if interested.

For Windows, save to a cmd file to run.

@ECHO OFF
SETLOCAL EnableDelayedExpansion 
SETLOCAL EnableExtensions

SET /A ecode=0

:: verify
SET /P conf="About to delete all global and local npm modules and clear the npm cache. Continue (y/[n])?
IF /I NOT "%conf%"=="y" (
  ECHO operation aborted
  SET /A ecode=!ecode!+1
  GOTO END
)

:: wipe global and local npm root
FOR %%a IN ("" "-g") DO (

  :: get root path into var
  SET cmd=npm root %%~a
  FOR /f "usebackq tokens=*" %%r IN (`!cmd!`) DO (SET npm_root=%%r)

  :: paranoid
  ECHO validating module path "!npm_root!"
  IF "!npm_root:~-12!"=="node_modules" (
    IF NOT EXIST "!npm_root!" (
      ECHO npm root does not exist "!npm_root!"
    ) ELSE (
      ECHO deleting "!npm_root!" ...
      :: delete
      RMDIR /S /Q "!npm_root!"
    )
  ) ELSE (
      ECHO suspicious npm root, ignoring "!npm_root!"
  )
)

:: clear the cache
ECHO clearing the npm cache ...
call npm cache clean

:: done
ECHO done

:END

ENDLOCAL & EXIT /b %ecode%
bvj
  • 3,294
  • 31
  • 30
4

All you done good job. This is combined suggestions in to one line code.

npm rm -g `npm ls -gp --depth=0 | awk -F/node_modules/ '{print $2}' | tr '/\n' ' '`

What is different? Uninstall will be done in single command like: npm rm -g *** *** ***

FDisk
  • 8,493
  • 2
  • 47
  • 52
4

For yarn global

nano ~/.config/yarn/global/package.json
<Manually remove all packages from package.json>
yarn global add

Or, if you don't care about what is actually inside package.json

echo {} >  ~/.config/yarn/global/package.json && yarn global add

This should apply to NPM too, but I am not exactly sure where NPM global is stored.

Polv
  • 1,918
  • 1
  • 20
  • 31
2

You can locate your all installed npm packages at the location:

C:\Users\username\AppData\Roaming\npm

and delete the content of npm which you want to remove.

If AppData is not showing, it means it is hidden and you can go to View in file explorer and checked the Hidden items then there you can see all the hidden folders.

Sachin
  • 1,206
  • 12
  • 13
2

For a more manual approach that doesn't involve an file explorers, doesn't care where the installation is, is very unlikely to break at a later date, and is 100% cross-platform compatible, and feels a lot safer because of the extra steps, use this one.

  • npm ls -g --depth=0
  • Copy output
  • Paste into favorite code editor (I use vsCode. Great multi-cursor editing)
  • Check for any packages you'd like to keep (nodemon, yarn, to name a few) Remove those lines
  • Remove every instance of +-- or other line decorators
  • Remove all the version information (eg '@2.11.4')
  • Put all items on same line, space separated
  • Add npm uninstall -g to beginning of that one line.
    • Mine looks like npm uninstall -g @angular/cli @vue/cli express-generator jest mocha typescript bindings nan nodemon yarn, but I didn't install many packages globally on this machine.
  • Copy line
  • Paste in terminal, hit enter if not already added from the copy/paste
  • Look for any errors in the terminal.
  • Check npm ls -g to make sure it's complete. If something got reinstalled, rinse and repeat

The other cli-only approaches are great for computer administrators doing something for 100 near-identical computers at once from the same ssh, or maybe a Puppet thing. But if you're only doing this once, or even 5 times over the course of a year, this is much easier.

RoboticRenaissance
  • 1,130
  • 1
  • 10
  • 17
2

For Windows:

rmdir /s /q "%appdata%/npm"
Minh Nguyễn
  • 63
  • 1
  • 1
  • 8
1

Well if you are on windows, and want to remove/uninstall all node_modules then you need to do following steps.

  1. Go to windows command prompt
  2. Navigate to node_modules directory (Not inside node_modules folder)
  3. Type below command and give it for 1-2 minutes it will uninstall all directories inside node_module

     rmdir /s /q node_modules
    

Hope this will help some one on windows

Anjum....
  • 4,086
  • 1
  • 35
  • 45
1

if you have Intellij Webstorm you can use its built-in graphical package manager.

open it as root and create an emtpy project. go to

File > Settings > Language and Frameworks > Node.js and NPM

there you will see all the installed packages. Uninstalling is easy, you can select and deselect any package you want to uninstall, Ctrl+a woks as well.

George Shalvashvili
  • 1,263
  • 13
  • 21
1

Simply use below for MAC,

sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man//node.}

mzonerz
  • 1,220
  • 15
  • 22
0

Use this code to uninstall any package:

npm rm -g <package_name>
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
user1452840
  • 221
  • 1
  • 4
  • 9
0
npm ls -gp | awk -F/ '/node_modules/&&!/node_modules.*node_modules/&&!/npm/{print $NF}' | xargs npm rm -g
rxw
  • 2,249
  • 3
  • 19
  • 14
0

Since this is the top answer in search I'm posting this here as it was the solution I used in the past to clean the computer switching laptops.

cd ~/Documents # or where you keep your projects
find . -name "node_modules" -exec rm -rf '{}' +

source: https://winsmarts.com/delete-all-node-modules-folders-recursively-on-windows-edcc9a9c079e

Giwan
  • 1,564
  • 14
  • 17
0

Here is a more elegant solution that I tried where I let npm do all the work for me.

# On Linux Mint 19.1 Cinnamon
# First navigate to where your global packages are installed.

$ npm root # returns /where/your/node_modules/folder/is
$ cd /where/your/node_modules/folder/is # i.e for me it was cd /home/user/.npm-packages/lib/node_modules

Then if you do npm uninstall or npm remove these modules will be treated as if they were normal dependencies of a project. It even generates a package-lock.json file when it is done:

$ npm remove <package-name> # you may need sudo if it was installed using sudo  
IskandarG
  • 323
  • 1
  • 3
  • 9
0

If you have have MSYS for Windows:

rm -rf ${APPDATA//\\/\/}/npm
mekb
  • 554
  • 8
  • 22
0

The npm README.md states:

If you would like to remove all the packages that you have installed, then you can use the npm ls command to find them, and then npm rm to remove them.

To remove cruft left behind by npm 0.x, you can use the included clean-old.sh script file. You can run it conveniently like this:

   npm explore npm -g -- sh scripts/clean-old.sh
scripter
  • 75
  • 2
  • 12
0

In macOS, I believe you can simply delete the .npm-global folder in your User directory.

.npm and .npm-global folders in macOS User directory:
.npm and .npm-global folders in macOS User directory

Dharman
  • 30,962
  • 25
  • 85
  • 135
0
npm list -g

will show you the location of globally installed packages.

If you want to output them to a file:
npm list -g > ~/Desktop/npmoutputs.txt

npm rm -g

will remove them

Palak Jain
  • 664
  • 6
  • 19
0
sudo npm uninstall npm -g

Or, if that fails, get the npm source code, and do:

sudo make uninstall

To remove everything npm-related manually:

rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/npm*
Kezai
  • 1
0

If you're using NVM for Windows, you need to delete all the modules that you don't want inside node_modules of the Node.js with the version that contains the global modules you want to remove. Do not remove corepack and npm packages as they are necessary for Node.js.

The folder can be located in:

  • %USERPROFILE%\.nvm\{version}\node_modules.

    • %USERPROFILE% is your user folder.
    • {version} is the version of Node.js where you want to delete its global modules.

    Example: C:\Users\Cappuccino\.nvm\19.8.1\node_modules.

  • {installationPath}\{version}\node_modules.

    • {installationPath} is where you have installed NVM for Windows.
    • {version} is the version of Node.js where you want to delete its global modules. Example: D:\Programs\NVM\v19.8.1\node_modules.
Cappuccino
  • 151
  • 3
  • 7
-1

sed solution

npm -gp ls | sed -r '/npm$|(node_modules.*){2,}/d; s:.*/([^/]+)$:\1:g' | xargs npm rm -g
koola
  • 1,616
  • 1
  • 13
  • 15
-5

Just put in your console:

sudo npm list -g --depth=0. | awk -F ' ' '{print $2}' | awk -F '@' '{print $1}' | sudo xargs npm remove -g

Its work for me...

busterkika
  • 3
  • 1
  • 5
  • 1
    Exact copt of solution already mentioned [here](http://stackoverflow.com/a/22290968/957731), on this very same page. – ivarni Jul 28 '16 at 10:10