[node.js] How do you uninstall all dependencies listed in package.json (NPM)?

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?

This question is related to node.js npm uninstallation

The answer is


(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.


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.


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

ls .\node_modules | % {npm uninstall $_}

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' ' '`

For windows go to node_modules dir and run this in powershell

npm uninstall (Get-ChildItem).Name 

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


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.


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

Second, run npm install

That simple.


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' ' '`

// 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.


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.


  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


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

Thanks @JustMailer


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.


Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to npm

What does 'x packages are looking for funding' mean when running `npm install`? error: This is probably not a problem with npm. There is likely additional logging output above Module not found: Error: Can't resolve 'core-js/es6' Browserslist: caniuse-lite is outdated. Please run next command `npm update caniuse-lite browserslist` ERROR in The Angular Compiler requires TypeScript >=3.1.1 and <3.2.0 but 3.2.1 was found instead DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean` What exactly is the 'react-scripts start' command? On npm install: Unhandled rejection Error: EACCES: permission denied Difference between npx and npm?

Examples related to uninstallation

How to uninstall Eclipse? How to remove docker completely from ubuntu 14.04 How to completely uninstall kubernetes How to uninstall Anaconda completely from macOS How to Completely Uninstall Xcode and Clear All Settings Remove composer How can I find the product GUID of an installed MSI setup? How to uninstall mini conda? python Force uninstall of Visual Studio How to remove a package from Laravel using composer?