88

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.

1
  • Since my dependencies get automatically added on package.json whenever I install with (--save) flag, I was wondering if there is a way to remove dependencies from package.json whenever I uninstall the package as well.
    – shriek
    Oct 16, 2013 at 23:19

2 Answers 2

155

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.

6
  • 3
    I'm using npm with ember-cli and I had to use --save-dev instead of --save.
    – gerry3
    Feb 6, 2015 at 6:56
  • 4
    if you installed it with --save-dev, then uninstall is also --save-dev, since --save saves into the dependencies section of the package.json file, whereas --save-dev will save into the devDependencies section Feb 6, 2015 at 19:00
  • 9
    I've come far since posting this question and here's a little pro-tip. You can use npm i <package_name> -S to add to your dependencies list or npm i <package_name> -D to add to your dev-dependencies. Same flag applies for removal with npm rm <package_name> -S or npm rm <package_name> -D
    – shriek
    Apr 2, 2015 at 0:16
  • 1
    but note that these are simply the shorthand flag equivalents for --save and --save-dev. See npm help for all flags and their shorthand equivalents. (anything installed with -S or --save and be uninstalled using either, too, npm does not see them as different. Just "less typing" for -S) Apr 27, 2015 at 17:42
  • 1
    You are a saint amongst men. Aug 17, 2017 at 5:29
1

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

1
  • 1
    This should not be necessary: if it's in the dependencies list than uninstall --save should remove it, regardless of whether something else depends on it. NPM uses rather clever dependency flattening so if you have X in your dependency list (explicitly) and you uninstall X --save, it will get removed. However, remember that there are multiple dependency lists, and --save is not the same as --save-dev, --save-prod,or --save-optional. Also yes: if your uninstall leads to errors, NPM stops at that error. It does not try to continue because it can't guarantee correctness anymore. Nov 23, 2018 at 19:08

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.