使用 npm 卸载时自动从 package.json 删除依赖项

npm init之后,我可以使用以下方法在 package.json 中添加依赖项:

npm install package --save

然后说,我想要 uninstall的包,我这样做:

npm uninstall package

但是我希望我的 package.json 也能相应地更新,而不需要我手动进入文件并删除那一行。

Npm 的文件上说:

它是严格附加的,所以它不会在没有充分理由的情况下从 package.json 中删除选项。

我只是想知道这是否可行。

44334 次浏览

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.

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