qodeninja qodeninja - 1 year ago 174
Node.js Question

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?

Answer Source

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!