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?
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!
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.
For windows go to node_modules dir and run this in powershell
npm uninstall (Get-ChildItem).Name
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.
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.
Tip for Windows users: Run this PowerShell command from within node_modules
parent directory:
ls .\node_modules | % {npm uninstall $_}
// 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.
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' ' '`
Powershell users:
foreach($package in ls node_modules){npm uninstall $package}
Thanks @JustMailer
- remove unwanted dependencies from package.json
npm i
"npm i
" will not only install missing deps, it updates node_modules to match the package.json
来源:https://stackoverflow.com/questions/19106284/how-do-you-uninstall-all-dependencies-listed-in-package-json-npm