当前位置: 首页 > 工具软件 > npm-consider > 使用案例 >

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

戚明朗
2023-12-01

本文翻译自:npm command to uninstall or prune unused packages in Node.js

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


#1楼

参考:https://stackoom.com/question/1RrXi/npm命令用于卸载或修剪Node-js中未使用的包


#2楼

如果你不担心几分钟的时间,那么解决方案就是rm -rf node_modulesnpm install再次重建本地模块。


#3楼

Run npm prune to 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 --production flag is specified, this command will remove the packages specified in your devDependencies. 如果指定了--production标志,则此命令将删除devDependencies中指定的包。


#4楼

You can use npm-prune to 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 --production flag is specified or the NODE_ENV environment variable is set to production , this command will remove the packages specified in your devDependencies . 如果指定了--production标志或将NODE_ENV环境变量设置为production ,则此命令将删除devDependencies中指定的包。 Setting --no-production will negate NODE_ENV being set to production . 设置--no-production将否定NODE_ENV被设置为生产

If the --dry-run flag is used then no changes will actually be made. 如果使用--dry-run标志,则实际上不会进行任何更改。

If the --json flag is used then the changes npm prune made (or would have made with --dry-run ) are printed as a JSON object. 如果使用了--json标志,那么修改后的npm修剪 (或者将使用--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 --production flag. 在启用了包锁的正常操作中,在安装模块时会自动修剪无关模块,并且您只需要使用带有--production标志的命令。

If you've disabled package-locks then extraneous modules will not be removed and it's up to you to run npm prune from 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 c@1.0.x
|    `-- c@1.0.3
`-- d <-- depends on c@~1.0.9
     `-- c@1.0.10

In this case, npm-dedupe will transform the tree to: 在这种情况下, npm-dedupe会将树转换为:

 a
 +-- b
 +-- d
 `-- c@1.0.10

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. 这将导致平面和重复数据删除树。

 类似资料: