node.js 使用 npm uninstall 时自动从 package.json 中删除依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19415523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:00:28  来源:igfitidea点击:

Automatically remove dependencies from package.json when using npm uninstall

node.jsnpm

提问by shriek

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

npm init我可以使用这个在我的 package.json 中添加依赖项之后:

npm install package --save

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

说,我想要uninstall包裹,我这样做:

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.

但我希望我的 package.json 也能相应地更新,而不必手动转到文件并删除该行。

From the npm docs it says:

从 npm 文档中它说:

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

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

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

所以,我只是想知道这是否可能。

回答by Mike 'Pomax' Kamermans

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

使用相同的--save标志。如果您安装了依赖项:

$> npm install grunt-cli --save

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

你可以卸载它, package.json 得到更新,使用:

$> npm uninstall grunt-cli --save

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

'save' 标志告诉 npm 根据您刚刚执行的操作更新 package.json。

回答by Naga

In my case --save did not clear the entry from package.json, the command as suggested by ionic-checkI 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

在我的情况下 --save 没有清除 package.json 中的条目,ionic-check建议的命令我认为如果卸载碰巧退出并出现任何错误 package.json 将不会更新,在这种情况下,您只有一个选项手动更改 package.json,这很乏味,但我猜这是唯一的方法

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

当您卸载依赖于其他活动包的包时,这种情况下卸载可能会失败并显示错误/警告,安全的方法是通过以下依赖关系图不确定是否有任何工具可用,此类操作下的一个方便的工具,警告尽管“您必须安装对等依赖项..”在我们卸载软件包时没有任何意义,但消息非常具有误导性