npm-view

查看注册表信息

选择命令行版本:

概要

🌐 Synopsis

npm view [<package-spec>] [<field>[.subfield]...]
aliases: info, show, v

描述

🌐 Description

此命令显示有关包的数据并将其打印到标准输出。

🌐 This command shows data about a package and prints it to stdout.

例如,要查看注册表中 connect 软件包的信息,你可以运行:

🌐 As an example, to view information about the connect package from the registry, you would run:

npm view connect

如果未指定,默认版本是 "latest"

🌐 The default version is "latest" if unspecified.

字段名称可以在包描述符之后指定。例如,要显示版本为 0.3.5ronn 包的依赖,可以执行以下操作:

🌐 Field names can be specified after the package descriptor. For example, to show the dependencies of the ronn package at version 0.3.5, you could do the following:

npm view ronn@0.3.5 dependencies

默认情况下,npm view 显示关于当前项目上下文的数据(通过查找 package.json)。要显示当前项目的字段数据,请使用文件路径(例如 .):

🌐 By default, npm view shows data about the current project context (by looking for a package.json). To show field data for the current project use a file path (i.e. .):

npm view . dependencies

你可以通过使用句点分隔来查看子字段。要查看 npm 最新版本的 git 仓库 URL,你可以运行以下命令:

🌐 You can view child fields by separating them with a period. To view the git repository URL for the latest version of npm, you would run the following command:

npm view npm repository.url

这使得通过一些 shell 脚本轻松查看关于依赖的信息。例如,要查看 ronn 所依赖的 opts 版本的所有数据,你可以编写如下内容:

🌐 This makes it easy to view information about a dependency with a bit of shell scripting. For example, to view all the data about the version of opts that ronn depends on, you could write the following:

npm view opts@$(npm view ronn dependencies.opts)

对于数组类型的字段,请求非数值字段将返回列表中对象的所有值。例如,要获取 express 包的所有贡献者电子邮件地址,你可以运行:

🌐 For fields that are arrays, requesting a non-numeric field will return all of the values from the objects in the list. For example, to get all the contributor email addresses for the express package, you would run:

npm view express contributors.email

你还可以在方括号中使用数字索引来专门选择数组字段中的某个项目。要获取列表中第一个贡献者的电子邮件地址,你可以运行:

🌐 You may also use numeric indices in square braces to specifically select an item in an array field. To just get the email address of the first contributor in the list, you can run:

npm view express contributors[0].email

如果你要查询的字段值是对象的属性,你应该运行:

🌐 If the field value you are querying for is a property of an object, you should run:

npm view express time'[4.8.0]'

注意:当访问包含特殊字符或数字键的对象属性时,需要在键名周围加上引号。例如,要获取特定版本的发布时间:

🌐 Note: When accessing object properties that contain special characters or numeric keys, you need to use quotes around the key name. For example, to get the publish time of a specific version:

npm view express "time[4.17.1]"

如果不加引号,shell 可能会将方括号解释为通配符模式,导致命令执行失败。你还可以通过在包描述符中指定版本来访问特定版本的时间字段:

🌐 Without quotes, the shell may interpret the square brackets as glob patterns, causing the command to fail. You can also access the time field for a specific version by specifying the version in the package descriptor:

npm view express@4.17.1 time

这将返回所有版本-时间对,但上下文仅限于该特定版本。

🌐 This will return all version-time pairs, but the context will be for that specific version.

可以指定多个字段,并将依次打印。例如,要获取所有贡献者的名称和电子邮件地址,可以这样做:

🌐 Multiple fields may be specified, and will be printed one after another. For example, to get all the contributor names and email addresses, you can do this:

npm view express contributors.name contributors.email

如果“Person”字段本应显示为对象,它们将显示为字符串。例如,这将以简化的字符串格式显示 npm 贡献者列表。(有关更多信息,请参见 package.json)。

npm view npm contributors

如果提供了版本范围,则会为每个匹配的包版本打印数据。这将显示每个匹配的 yui3 版本所需的 jsdom 版本:

🌐 If a version range is provided, then data will be printed for every matching version of the package. This will show which version of jsdom was required by each matching version of yui3:

npm view yui3@'>0.5.4' dependencies.jsdom

要显示 connect 软件包的版本历史,可以这样做:

🌐 To show the connect package version history, you can do this:

npm view connect versions

字段访问模式

🌐 Field Access Patterns

npm view 命令支持以不同方式访问包元数据中的嵌套字段和数组元素。理解这些模式可以更容易地提取特定信息。

🌐 The npm view command supports different ways to access nested fields and array elements in package metadata. Understanding these patterns makes it easier to extract specific information.

嵌套对象字段

🌐 Nested Object Fields

使用点号表示法访问嵌套对象字段:

🌐 Use dot notation to access nested object fields:

# Access nested properties
npm view npm repository.url
npm view express bugs.url

数组元素访问

🌐 Array Element Access

对于数组,请使用方括号中的数字索引访问特定元素:

🌐 For arrays, use numeric indices in square brackets to access specific elements:

# Get the first contributor's email
npm view express contributors[0].email
# Get the second maintainer's name
npm view express maintainers[1].name

对象属性访问

🌐 Object Property Access

对于对象属性(例如访问 time 字段中的特定版本),请使用带引号的方括号表示法访问属性名:

🌐 For object properties (like accessing specific versions in the time field), use bracket notation with the property name in quotes:

# Get publish time for a specific version
npm view express "time[4.17.1]"
# Get dist-tags
npm view express "dist-tags.latest"

从数组中提取字段

🌐 Extracting Fields from Arrays

请求数组中的非数字字段以获取列表中所有对象的值:

🌐 Request a non-numeric field on an array to get all values from objects in the list:

# Get all contributor emails
npm view express contributors.email
# Get all contributor names
npm view express contributors.name

配置

🌐 Configuration

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.

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.

输出

🌐 Output

如果仅输出单个版本的单个字符串字段,那么它将不会被着色或加引号,以便将输出传递给另一个命令。如果该字段是一个对象,它将以 JavaScript 对象字面量的形式输出。

🌐 If only a single string field for a single version is output, then it will not be colorized or quoted, to enable piping the output to another command. If the field is an object, it will be output as a JavaScript object literal.

如果提供了 --json 标志,输出的字段将是 JSON。

🌐 If the --json flag is given, the outputted fields will be JSON.

如果版本范围匹配多个版本,则每个打印的值都将以其适用的版本为前缀。

🌐 If the version range matches multiple versions then each printed value will be prefixed with the version it applies to.

如果请求多个字段,则每个字段都以字段名称为前缀。

🌐 If multiple fields are requested, then each of them is prefixed with the field name.

也可以看看

🌐 See Also