Is there a command to remove all global npm modules? If not, what do you suggest?
31 Answers
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=0lists 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 rmremoves all modules globally that come over the previous pipe
18 Comments
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 npmFor 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.
8 Comments
npm install again. I had to delete my package-lock.json file as well.C:\Users\[username]\AppData\Roaming\nvm\[version]\node_modulesI 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.
13 Comments
npm installs all its global modules to the same directory. The exact location may vary, but typically it's /usr/local/lib/node_modules.npm ls -gp --depth=0 | awk -F/node_modules/ '{print $2}' | grep -vE '^(npm)$' | xargs npm -g rm@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 rmsudo 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 installedawk -F ' ' '{print $2}'gets rid of ├──awk -F '@' '{print $1}'gets the part before '@'sudo xargs npm remove -gremoves the package globally
4 Comments
npm remove UNMET simply does NOOP.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 -gJust 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)?
1 Comment
npm and n before the trin windows go to "C:\Users{username}\AppData\Roaming" directory and manually remove npm folder
1 Comment
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 2 Comments
json style.npm ls -g --json --depth=0 | jq -r '.dependencies|keys-["npm"]|join("\n")' | xargs sudo npm rm -gAll 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 *** *** ***
1 Comment
asdf uninstall nodejs <version>.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% Comments
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.
1 Comment
yarn global remove $(yarn global list | grep info | sed 's/^info "\(.*\)@.*".*$/\1/') (Found here github.com/yarnpkg/yarn/issues/1048#issuecomment-758291289)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 -gto 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.
- Mine looks like
- 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 -gto 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.
Comments
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.
Comments
Well if you are on windows, and want to remove/uninstall all node_modules then you need to do following steps.
- Go to windows command prompt
- Navigate to node_modules directory (Not inside node_modules folder)
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
Comments
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.
Comments
Try this and tell me what you think
npx npkill 1 Comment
Use this code to uninstall any package:
npm rm -g <package_name> 1 Comment
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
Comments
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 Comments
The npm README.md states:
If you would like to remove all the packages that you have installed, then you can use the
npm lscommand to find them, and thennpm rmto remove them.To remove cruft left behind by npm 0.x, you can use the included
clean-old.shscript file. You can run it conveniently like this:npm explore npm -g -- sh scripts/clean-old.sh
Comments
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.
