Automatically remove dependencies from package.json when using npm uninstall

node.jsNpm

node.js Problem Overview


After npm init I can add dependencies in my package.json using this:

npm install package --save

And say, I want to uninstall the package and I do so by doing:

npm uninstall package

but I want my package.json to be updated accordingly too without me having to manually go to the file and delete that line.

From the npm docs it says:

> It is strictly additive, so it does not delete options from your package.json without a really good reason to do so.

So, I just wanted to know if this is even possible.

node.js Solutions


Solution 1 - node.js

Use the same --save flag. If you installed a dependency with:

$> npm install grunt-cli --save

you can uninstall it, with package.json getting updated, using:

$> npm uninstall grunt-cli --save

The 'save' flag tells npm to update package.json based on the operation you just made it do.

Solution 2 - node.js

In my case --save did not clear the entry from package.json, the command as suggested by ionic-check I think if the uninstall happens to exit with any errors package.json will not be updated in which case you only have an option to manually change package.json, this is tedious but the only way I guess

UPDATE

when you uninstall a package which has a dependency on other package which is active then which case uninstall may fail with errors/warnings, the safe method is through following dependency graph not sure if there any tool available, a handy tool under such operations, warning messages are quite misleading though "you must install peer dependencies.." doesn't make any sense when we are uninstalling a package

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshriekView Question on Stackoverflow
Solution 1 - node.jsMike 'Pomax' KamermansView Answer on Stackoverflow
Solution 2 - node.jsNagaView Answer on Stackoverflow