141

If I have a package.json file defined in my application root and run npm install -g it will install all the dependencies defined in package.json, globablly.

However, this doesn't seem to work in reverse.

If I do npm uninstall -g in my application root it throws an error, expceting me to pass it a package name.

Shouldn't this also uninstall the same packages I installed?

Am I doing something wrong?

qodeninja
  • 10,946
  • 30
  • 98
  • 152
  • 1
    FYI my reasoning behind never using npm -g. http://peterlyons.com/problog/2012/09/managing-per-project-interpreters-and-the-path – Peter Lyons Oct 01 '13 at 04:43
  • 1
    Remember that if you want to use a module for your project, you have to install it locally. Installing a module globally only provides more commands in your terminal, as with expressjs and express(1) for instance. – warchimede Oct 01 '13 at 10:47
  • @sgwilly You're wrong about having to use node_modules globally -- it even says so in the docuemention - http://nodejs.org/api/modules.html#modules_all_together - the require() function is 'smart' in that it looks in various location to try and resolve a path for a module including the global node_modules – qodeninja Oct 01 '13 at 16:39
  • I should have been more specific : it's true that require() is smart enough to find your globally installed module. It'll work if all your projects depend on the same version of that said module. Yet, you might have a hard time maintaining projects relying on different module versions if you only manage dependencies globally. That's why it's best practice to always install modules locally, and only install them globally when you want to get access to some handy executable commands. – warchimede Oct 02 '13 at 09:49
  • @sgwilly - thanks for sharing your thoughts here -- this project in particular is a distributable so it should not be trying to update anything -- the actually updates will happen over RPM (note RPM not NPM) and the actual node_modules are shrinkwrapped -- I just needed a way to quickly clean the system of whatever the rpm ended up installing (in my dev environment) – qodeninja Oct 02 '13 at 18:42

16 Answers16

196

If using Bash, just switch into the folder that has your package.json file and run the following:

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

In the case of globally-installed packages, switch into your %appdata%/npm folder (if on Windows) and run the same command.

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

jedmao
  • 10,224
  • 11
  • 59
  • 65
  • 1
    You posted the exact same command as an answer to "Command to remove all npm modules globally?". How can this be the appropriate thing to do in both that situation and this one? – nobody Jun 24 '14 at 21:26
  • @AndrewMedico How is it not appropriate? Packages installed in your package.json file will be installed in your node_modules folder. This command will take every sub-folder to the node_modules folder and run `npm uninstall ` for each folder. I did this myself and it works excellently! – jedmao Jun 24 '14 at 21:56
  • @AndrewMedico I just edited the answer to include instructions for global uninstall. – jedmao Jun 24 '14 at 22:00
  • why not simply delete the node_modules folder ? – Ugo Robain Sep 21 '14 at 22:16
  • 2
    @UgoRobain good question. As it turns out, some dependencies get really deep. On the NTFS file system (Windows), at least, it has problems removing directories where the path name is longer than some kind of limit, leaving these phantom directories that you can't remove. Fortunately, `npm uninstall` takes care of that, which is why this trick is useful. – jedmao Sep 22 '14 at 23:45
  • 8
    Here's what I used to do it with powershell: `foreach($package in ls node_modules){npm uninstall $package}` – JustMaier Apr 16 '15 at 17:47
  • Pretty sure you don't need bash for what you just suggested if you're on Unix/Mac/Linux; instead, just try `ls node_modules | xargs npm uninstall` – elreimundo Apr 30 '15 at 19:30
  • PowerShell from @jedmao needs to be foreach($package in ls node_modules){npm uninstall $package.Name} instead of just $package – AlignedDev Oct 07 '15 at 18:21
  • 1
    doesn't seem to work on windows. `tr is not recognized` – Overdrivr Feb 03 '16 at 08:38
  • @Overdrivr if you have GitHub for Windows installed, just set your PATH to "%appdata%/../local/GitHub/PortableGit_xxx/user/bin" and you're good to go! – jedmao Mar 06 '16 at 16:39
  • This erased the packages from my package.json dependencies and devDependencies :/ – mattyb Jun 11 '17 at 01:09
108

This worked for me:

command prompt or gitbash into the node_modules folder in your project then execute:

npm uninstall *

Removed all of the local packages for that project.

Druubacca
  • 1,115
  • 1
  • 7
  • 4
  • Doesn't this remove global installs too? – shinzou Jul 31 '18 at 12:21
  • 2
    @shinzou It did not remove global installs for me. – Taylor D. Edmiston Aug 02 '18 at 20:15
  • 2
    On Mac 10.13.5, this throws lots of error as below "> heapdump@0.3.9 install /Users/vikramanna/node_modules/heapdump > node-gyp rebuild CXX(target) Release/obj.target/addon/src/heapdump.o In file included from ../src/heapdump.cc:17: ../src/compat-inl.h:300:19: error: no member named 'GetCpuProfiler' in 'v8::Isolate' return isolate->GetCpuProfiler()->StartProfiling(title, record_samples);" – vikramvi Oct 29 '18 at 06:29
40

I recently found a node command that allows uninstalling all the development dependencies as follows:

npm prune --production

As I mentioned, this command only uninstalls the development dependency packages. At least it helped me not to have to do it manually.

brismuth
  • 36,149
  • 3
  • 34
  • 37
chalo
  • 1,019
  • 2
  • 14
  • 27
  • This simply CANNOT be the command to "remove all modules". This command removes UNUSED development dependencies that are not required for PRODUCTION (--production). If it's not explicitly listed under "dependencies" or "optionalDependencies" sections of your package.json file, it's gets removed. – E10 May 02 '23 at 12:01
39

For windows go to node_modules dir and run this in powershell

npm uninstall (Get-ChildItem).Name 
Sameer
  • 3,124
  • 5
  • 30
  • 57
10

Tip for Windows users: Run this PowerShell command from within node_modules parent directory:

ls .\node_modules | % {npm uninstall $_}
y0n1
  • 166
  • 1
  • 9
9
// forcibly remove and reinstall all package dependencies
ren package.json package.json-bak
echo {} > package.json
npm prune
del package.json
ren package.json-bak package.json
npm i

This essentially creates a fake, empty package.json, calls npm prune to remove everything in node_modules, restores the original package.json and re-installs everything.

Some of the other solutions might be more elegant, but I suspect this is faster and exhaustive. On other threads I've seen people suggest just deleting the node_modules directory, but at least for windows, this causes npm to choke afterward because the bin directory goes missing. Maybe on linux it gets restored properly, but not windows.

Terry Young
  • 3,531
  • 1
  • 20
  • 21
meem
  • 211
  • 3
  • 5
  • 1
    In my case - I did not need the existing package.json (because of running the script on CI after npm scripts were executed..) So I just went with: "echo {} > package.json & npm prune" – Rikki Jul 31 '17 at 13:29
9

Another SIMPLE option is to delete the node_modules and package-lock.json

rm -rf node_modules
rm -rf package-lock.json

After this you can try reinstalling the npm packages

Harsh Phoujdar
  • 670
  • 10
  • 22
  • 2
    Thanks for this suggestion. I had no global packages. Most responses are talking about -g option! I had horrible time getting rid of ALL local packages because not knowing enough about nodejs/npm, I did 'npm install this-and-that' from several different directories. When I use 'npm uninstall ', the 'npm ls' still shows some packages. I am shell programmer so tried 'npm uninstall' on every imaginable part of the 'npm ls' output. None worked to get rid of ALL packages. Tired, but found this suggestion and fixed my issue in 5 minutes. Thanks Harsh. – BReddy Aug 18 '20 at 21:44
  • 1
    I don't know what the purpose of package.json, package-lock.json, node_modules, dependencies, local/global differences, or how nodejs/npm works. I use a nodejs module that is needed for another program. Since I installed many different modules with 'npm install' from different directories, I wanted to get rid of ALL of them at once. None of the other answers are talking about this. Using this and 'find' command on linux, I was able to get rid of everything in 5 minutes. Don't know why nobody else found this helpful yet. – BReddy Aug 18 '20 at 21:51
6

Actually there is no option to do that, if you want to uninstall packages from package.json simply do npm ls on the same directory that package.json relies and use npm uninstall <name> or npm rm <name> for the package you want to remove.

Gntem
  • 6,949
  • 2
  • 35
  • 48
  • 9
    I know how to remove them individually -- it would just make more sense if there was a command to run against the package.json file the same way npm install works. – qodeninja Oct 01 '13 at 16:39
  • No we don't want to uninstall by hand every module. – shinzou Jul 31 '18 at 12:13
6

First, remove all packages from dependencies and devDependencies in package.json

Second, run npm install

That simple.

A. Khaled
  • 1,468
  • 16
  • 28
4
  1. remove unwanted dependencies from package.json
  2. npm i

"npm i" will not only install missing deps, it updates node_modules to match the package.json

Lakmi
  • 1,379
  • 3
  • 16
  • 27
2

Powershell users: foreach($package in ls node_modules){npm uninstall $package}

Thanks @JustMailer

Valentine Bondar
  • 139
  • 2
  • 10
2

(Don't replicate these steps till you read everything)

For me all mentioned solutions didn't work. Soo I went to /usr/lib and run there

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

But it also removed the npm package and only half of the packages (till it reached letter n).

So I tried to install node again by the node guide.

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs

But it didn't install npm again.

So I decided to reinstall whole node sudo apt-get remove nodejs And again install by the guide above.

Now is NPM again working but the global modules are still there. So I checked the content of the directory /usr/lib/node_modules and seems the only important here is npm. So I edited the command above to uninstall everything except npm

for package in $(ls node_modules); do if [ "$package" != "npm" ]; then sudo npm uninstall $package; fi; done;

It removed all modules what were not prefixed @. Soo I extended the loop for subdirectories.

for package in $(ls node_modules); do if [  ${package:0:1} = \@ ]; then 
        for innerPackage in $(ls node_modules/${package}); do
                sudo npm uninstall "$package/$innerPackage";
        done;
fi; done;

My /usr/lib/node_modules now contains only npm and linked packages.

siOnzee
  • 91
  • 8
1

Even you don't need to run the loop for that.

You can delete all the node_modules by using the only single command:-

npm uninstall `ls -1 node_modules | tr '/\n' ' '`
VIKAS KOHLI
  • 8,164
  • 4
  • 50
  • 61
1

Piggy-backing off of VIKAS KOHLI and jedmao, you can do this

single line version:

npm uninstall `ls -1 node_modules | grep -v ^@ | tr '/\n' ' '` `find node_modules/@* -type d -depth 1 2>/dev/null | cut -d/ -f2-3 | tr '\n' ' '`

multi-lined version:

npm uninstall \
`ls -1 node_modules | grep -v ^@ | tr '/\n' ' '` \
`find node_modules/@* -type d -depth 1 2>/dev/null | cut -d/ -f2-3 | tr '\n' ' '`
Ryan Lecha
  • 91
  • 1
  • 7
1

Since this is still the first result on the Googler when searching how to remove all modules in NPM, I figured I'd share a small script for Powershell to remove all dependencies through NPM:

#Create a Packages Array to add package names to
$Packages = New-Object System.Collections.ArrayList

#Get all Production Dependencies by name
(Get-Content .\Package.json | ConvertFrom-JSON).dependencies.psobject.properties.name |
ForEach-Object { $Packages.Add($_) | Out-Null }

#Get all Dev Dependencies by name
(Get-Content .\Package.json | ConvertFrom-JSON).devDependencies.psobject.properties.name | 
ForEach-Object { $Packages.Add($_) | Out-Null }

#Remove each package individually
Foreach($Package in ($Packages | select -unique))
{ npm uninstall $Package }

#Clean up any remaining packages
$Modules = Get-ChildItem "node_modules"
if($Modules)
{ $Modules | ForEach-Object { Remove-Item ".\node_modules\$_" -Force -Recurse } }

This runs a more specific removal, rather than removing each module from node_modules individually.

0

You need simply to remove the node_modules folder, navigate to your angular app folder then run one of this command.

  • On windows :
rmdir /s node_modules 
  • On linux :
rm -r node_modules/
Tyler2P
  • 2,324
  • 26
  • 22
  • 31