npm-ls

列出已安装的包

选择命令行版本:

概要

🌐 Synopsis

npm ls <package-spec>
alias: list

描述

🌐 Description

此命令将在指定 --all 时,将所有已安装软件包的版本及其依赖以树状结构打印到标准输出。

🌐 This command will print to stdout all the versions of packages that are installed, as well as their dependencies when --all is specified, in a tree structure.

注意:要了解某个软件包为何被包含在树中,可以使用 npm explain 查看“底层视图”。

🌐 Note: to get a "bottoms up" view of why a given package is included in the tree at all, use npm explain.

位置参数是 name@version-range 标识符,它们将把结果限制为仅显示指定名称的包的路径。注意,嵌套包也会显示指定包的路径。例如,在 npm 的源代码树中运行 npm ls promzard 将显示:

🌐 Positional arguments are name@version-range identifiers, which will limit the results to only the paths to the packages named. Note that nested packages will also show the paths to the specified packages. For example, running npm ls promzard in npm's source tree will show:

npm@9.9.4 /path/to/npm
└─┬ init-package-json@0.0.4
└── promzard@0.1.5

它将打印出无关的、丢失的和无效的包。

🌐 It will print out extraneous, missing, and invalid packages.

如果一个项目为依赖指定了 git URL,这些 URL 会显示在 name@version 之后的括号中,以便用户更容易识别项目的潜在分支。

🌐 If a project specifies git urls for dependencies these are shown in parentheses after the name@version to make it easier for users to recognize potential forks of a project.

所示的树是逻辑依赖树,基于包依赖,而不是你 node_modules 文件夹的物理布局。

🌐 The tree shown is the logical dependency tree, based on package dependencies, not the physical layout of your node_modules folder.

当以 llla 运行时,默认会显示扩展信息。

🌐 When run as ll or la, it shows extended information by default.

注意:设计变更待定

🌐 Note: Design Changes Pending

npm ls 命令的输出和行为在 npm 创建一个天真地将所有依赖嵌套的 node_modules 文件夹时非常有意义。在这种情况下,逻辑依赖图和磁盘上包的物理树大致相同。

🌐 The npm ls command's output and behavior made a ton of sense when npm created a node_modules folder that naively nested every dependency. In such a case, the logical dependency graph and physical tree of packages on disk would be roughly identical.

随着 npm v3 自动安装时去重依赖的出现,ls 输出被修改为以树状结构显示逻辑依赖图,因为这对大多数用户更有用。然而,如果不使用 npm ls -l,通常就无法显示包实际安装的位置!

🌐 With the advent of automatic install-time deduplication of dependencies in npm v3, the ls output was modified to display the logical dependency graph as a tree structure, since this was more useful to most users. However, without using npm ls -l, it became impossible to show where a package was actually installed much of the time!

随着 npm v7 自动安装 peerDependencies 的出现,这就更加令人好奇了,因为在依赖图中,peerDependencies 逻辑上位于其依赖“之下”,但在磁盘上的位置总是位于其依赖的同一层或更上方。

🌐 With the advent of automatic installation of peerDependencies in npm v7, this gets even more curious, as peerDependencies are logically "underneath" their dependents in the dependency graph, but are always physically at or above their location on disk.

此外,自从 npm 在版本 0.0.2 引入 ls 命令以来,依赖图总体上变得更加庞大。因此,为了避免向终端输出过多内容,npm ls 现在只显示 顶层 依赖,除非提供了 --all

🌐 Also, in the years since npm got an ls command (in version 0.0.2!), dependency graphs have gotten much larger as a general rule. Therefore, in order to avoid dumping an excessive amount of content to the terminal, npm ls now only shows the top level dependencies, unless --all is provided.

目前正在对该命令的使用场景、意图、行为和输出进行彻底的重新审查。预计在 npm v8 中,至少默认的可读性 npm ls 输出将会有重大变化。

🌐 A thorough re-examination of the use cases, intention, behavior, and output of this command, is currently underway. Expect significant changes to at least the default human-readable npm ls output in npm v8.

配置

🌐 Configuration

all

  • 默认:否
  • 类型:布尔

在运行 npm outdatednpm ls 时,设置 --all 将显示所有过时或已安装的软件包,而不仅仅是当前项目直接依赖的那些包。

🌐 When running npm outdated and npm ls, setting --all will show all outdated or installed packages, rather than only those directly depended upon by the current project.

json

  • 默认:否
  • 类型:布尔

是否输出 JSON 数据,而不是正常输出。

🌐 Whether or not to output JSON data, rather than the normal output.

  • npm pkg set 中,它可以在将值保存到你的 package.json 之前使用 JSON.parse() 解析设置的值。

并非所有 npm 命令都支持。

🌐 Not supported by all npm commands.

long

  • 默认:否
  • 类型:布尔

lssearchhelp-search 中显示扩展信息。

🌐 Show extended information in ls, search, and help-search.

parseable

  • 默认:否
  • 类型:布尔

从写入标准输出的命令中输出可解析的结果。对于 npm search,这将是制表符分隔的表格格式。

🌐 Output parseable results from commands that write to standard output. For npm search, this will be tab-separated table format.

global

  • 默认:否
  • 类型:布尔

以“全局”模式运行,因此软件包会安装到 prefix 文件夹中,而不是当前工作目录。有关行为差异的更多信息,请参见 folders

🌐 Operates in "global" mode, so that packages are installed into the prefix folder instead of the current working directory. See folders for more on the differences in behavior.

  • 软件包被安装到 {prefix}/lib/node_modules 文件夹中,而不是当前工作目录。
  • bin 文件与 {prefix}/bin 关联
  • 手册页已链接到 {prefix}/share/man

depth

  • 默认:如果设置了 --all,则为 Infinity,否则为 1
  • 类型:空或数字

在递归 npm ls 包时要达到的深度。

🌐 The depth to go when recursing packages for npm ls.

如果未设置,npm ls 只会显示根项目的直接依赖。如果设置了 --all,则 npm 默认会显示所有依赖。

🌐 If not set, npm ls will show only the immediate dependencies of the root project. If --all is set, then npm will show all dependencies by default.

omit

  • 默认值:如果 NODE_ENV 环境变量设置为 'production',则为 'dev',否则为空。
  • 类型:“dev”、“optional” 或 “peer”(可以设置多次)

要从磁盘上的安装树中省略的依赖类型。

🌐 Dependency types to omit from the installation tree on disk.

请注意,这些依赖仍然会被解析并添加到 package-lock.jsonnpm-shrinkwrap.json 文件中。它们只是没有实际安装到磁盘上。

🌐 Note that these dependencies are still resolved and added to the package-lock.json or npm-shrinkwrap.json file. They are just not physically installed on disk.

如果某种封装类型同时出现在 --include--omit 列表中,那么它将被包括在内。

🌐 If a package type appears in both the --include and --omit lists, then it will be included.

如果生成的省略列表包含 'dev',那么所有生命周期脚本的 NODE_ENV 环境变量将被设置为 'production'

🌐 If the resulting omit list includes 'dev', then the NODE_ENV environment variable will be set to 'production' for all lifecycle scripts.

  • 默认:否
  • 类型:布尔

npm ls 一起使用,仅限制输出到那些已链接的包。

🌐 Used with npm ls, limiting output to only those packages that are linked.

package-lock-only

  • 默认:否
  • 类型:布尔

如果设置为 true,当前操作将只使用 package-lock.json,忽略 node_modules

🌐 If set to true, the current operation will only use the package-lock.json, ignoring node_modules.

对于 update,这意味着只会更新 package-lock.json,而不会检查 node_modules 或下载依赖。

🌐 For update this means only the package-lock.json will be updated, instead of checking node_modules and downloading dependencies.

对于 list,这意味着输出将基于 package-lock.json 描述的树,而不是 node_modules 的内容。

🌐 For list this means the output will be based on the tree described by the package-lock.json, rather than the contents of node_modules.

unicode

  • 默认值:在 Windows 上为 false,在 mac/unix 系统上具有 Unicode 本地化环境时为 true,这由 LC_ALLLC_CTYPELANG 环境变量定义。
  • 类型:布尔

当设置为 true 时,npm 在树状输出中使用 Unicode 字符。当设置为 false 时,它使用 ASCII 字符而不是 Unicode 字符。

🌐 When set to true, npm uses unicode characters in the tree output. When false, it uses ascii characters instead of unicode glyphs.

workspace

  • 默认值:
  • 类型:字符串(可以多次设置)

启用在当前项目的已配置工作区的上下文中运行命令,同时通过仅运行此配置选项定义的工作区进行过滤。

🌐 Enable running a command in the context of the configured workspaces of the current project while filtering by running only the workspaces defined by this configuration option.

workspace 配置的有效值为以下之一:

🌐 Valid values for the workspace config are either:

  • 工作区名称
  • 工作区目录的路径
  • 父工作区目录的路径(将导致选择该文件夹中的所有工作区)

对于 npm init 命令设置时,可以将其设置为一个尚不存在的工作区文件夹,以创建该文件夹并将其作为项目内全新的工作区进行设置。

🌐 When set for the npm init command, this may be set to the folder of a workspace which does not yet exist, to create the folder and set it up as a brand new workspace within the project.

此值不会导出到子进程的环境中。

🌐 This value is not exported to the environment for child processes.

workspaces

  • 默认值:空
  • 类型:空或布尔值

设置为 true 以在 所有 配置的工作区上下文中运行命令。

🌐 Set to true to run the command in the context of all configured workspaces.

将此显式设置为 false 会导致像 install 这样的命令完全忽略工作区。如果不显式设置:

🌐 Explicitly setting this to false will cause commands like install to ignore workspaces altogether. When not set explicitly:

  • node_modules 树操作的命令(安装、更新等)会将工作区链接到 node_modules 文件夹。- 执行其他操作的命令(测试、执行、发布等)会在根项目上运行,除非workspace 配置中指定了一个或多个工作区。

此值不会导出到子进程的环境中。

🌐 This value is not exported to the environment for child processes.

include-workspace-root

  • 默认:否
  • 类型:布尔

为命令启用工作区时包括工作区根。

🌐 Include the workspace root when workspaces are enabled for a command.

当为 false 时,通过 workspace 配置指定单个工作区,或通过 workspaces 标志指定所有工作区,将导致 npm 仅在指定的工作区上操作,而不会在根项目上操作。

🌐 When false, specifying individual workspaces via the workspace config, or all workspaces via the workspaces flag, will cause npm to operate only on the specified workspaces, and not on the root project.

此值不会导出到子进程的环境中。

🌐 This value is not exported to the environment for child processes.

  • 默认:否
  • 类型:布尔

当设置文件时:协议依赖将作为常规依赖被打包和安装,而不会创建符号链接。此选项对工作区没有影响。

🌐 When set file: protocol dependencies will be packed and installed as regular dependencies instead of creating a symlink. This option has no effect on workspaces.

也可以看看

🌐 See Also