用于卸载或修剪 Node.js 中未使用的包的 npm 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21417014/
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:46:36  来源:igfitidea点击:

npm command to uninstall or prune unused packages in Node.js

node.jsnpmuninstall

提问by Tarion

Is there a way to simply uninstall all unused (undeclared) dependencies from a Node.js project (ones that are no longer defined in my package.json.) When I update my application I like to have the unreferenced packages removed automatically.

有没有一种方法可以简单地从 Node.js 项目(不再在我的 .js 中定义的那些)卸载所有未使用的(未声明的)依赖项package.json。当我更新我的应用程序时,我喜欢自动删除未引用的包。

回答by Darkhogg

Note: Recent npmversions do this automatically when package-locks are enabled, so this is not necessary except for removing development packages with the --productionflag.

注意npm当启用包锁定时,最近的版本会自动执行此操作,因此除了删除带有--production标志的开发包外,这不是必需的。



Run npm pruneto remove modules not listed in package.json.

运行npm prune以删除 中未列出的模块package.json

From npm help prune:

来自npm help prune

This command removes "extraneous" packages. If a package name is provided, then only packages matching one of the supplied names are removed.

Extraneous packages are packages that are not listed on the parent package's dependencies list.

If the --productionflag is specified, this command will remove the packages specified in your devDependencies.

此命令删除“无关的”包。如果提供了包名称,则仅删除与提供的名称之一匹配的包。

无关包是未在父包的依赖项列表中列出的包。

如果--production指定了标志,此命令将删除您的 devDependencies 中指定的包。

回答by Pyrce

If you're not worried about a couple minutes time to do so, a solution would be to rm -rf node_modulesand npm installagain to rebuild the local modules.

如果你不担心一两分钟的时间这样做,一个解决办法是rm -rf node_modulesnpm install再次来重建本地模块。

回答by Igor Litvinovich

You can use npm-pruneto remove extraneous packages.

您可以使用npm-prune删除无关的包。

npm prune [[<@scope>/]<pkg>...] [--production] [--dry-run] [--json]

This command removes "extraneous" packages. If a package name is provided, then only packages matching one of the supplied names are removed.

此命令删除“无关的”包。如果提供了包名称,则仅删除与提供的名称之一匹配的包。

Extraneous packages are packages that are not listed on the parent package's dependencies list.

无关包是未在父包的依赖项列表中列出的包。

If the --productionflag is specified or the NODE_ENVenvironment variable is set to production, this command will remove the packages specified in your devDependencies. Setting --no-productionwill negate NODE_ENVbeing set to production.

如果指定了--production标志或NODE_ENV环境变量设置为production,此命令将删除devDependencies 中指定的包。设置--no-production将否定NODE_ENV被设置为production

If the --dry-runflag is used then no changes will actually be made.

如果使用--dry-run标志,则实际上不会进行任何更改。

If the --jsonflag is used then the changes npm prunemade (or would have made with --dry-run) are printed as a JSON object.

如果使用--json标志,则npm prune所做的更改(或使用--dry-run所做的更改)将打印为 JSON 对象。

In normal operation with package-locks enabled, extraneous modules are pruned automatically when modules are installed and you'll only need this command with the --productionflag.

在启用包锁的正常操作中,安装模块时会自动修剪无关的模块,您只需要带有--production标志的命令。

If you've disabled package-locks then extraneous modules will not be removed and it's up to you to run npm prunefrom time-to-time to remove them.

如果您禁用了包锁,则不会删除无关的模块,您可以不时运行npm prune来删除它们。

Use npm-dedupe to reduce duplication

使用 npm-dedupe 减少重复

npm dedupe
npm ddp

Searches the local package tree and attempts to simplify the overall structure by moving dependencies further up the tree, where they can be more effectively shared by multiple dependent packages.

搜索本地包树并尝试通过将依赖项进一步向上移动树来简化整体结构,在那里它们可以被多个依赖包更有效地共享。

For example, consider this dependency graph:

例如,考虑这个依赖图:

a
+-- b <-- depends on [email protected]
|    `-- [email protected]
`-- d <-- depends on c@~1.0.9
     `-- [email protected]

In this case, npm-dedupewill transform the tree to:

在这种情况下,npm-dedupe会将树转换为:

 a
 +-- b
 +-- d
 `-- [email protected]

Because of the hierarchical nature of node's module lookup, b and d will both get their dependency met by the single c package at the root level of the tree.

由于节点模块查找的层次性质,b 和 d 都将通过树根级别的单个 c 包满足它们的依赖关系。

The deduplication algorithm walks the tree, moving each dependency as far up in the tree as possible, even if duplicates are not found. This will result in both a flat and deduplicated tree.

重复数据删除算法遍历树,在树中尽可能向上移动每个依赖项,即使没有找到重复项。这将导致平坦和重复数据删除的树。