目录
选择命令行版本:
目录
概要
¥Synopsis
npm query <selector>
描述
¥Description
npm query
命令允许使用 css 选择器来检索依赖对象数组。
¥The npm query
command allows for usage of css selectors in order to retrieve an array of dependency objects.
管道 npm 查询到其他命令
¥Piping npm query to other commands
# find all dependencies with postinstall scripts & uninstall themnpm query ":attr(scripts, [postinstall])" | jq 'map(.name)|join("\n")' -r | xargs -I {} npm uninstall {}# find all git dependencies & explain who requires themnpm query ":type(git)" | jq 'map(.name)' | xargs -I {} npm why {}
扩展用例和查询
¥Extended Use Cases & Queries
// all deps*// all direct deps:root > *// direct production deps:root > .prod// direct development deps:root > .dev// any peer dep of a direct deps:root > * > .peer// any workspace dep.workspace// all workspaces that depend on another workspace.workspace > .workspace// all workspaces that have peer deps.workspace:has(.peer)// any dep named "lodash"// equivalent to [name="lodash"]#lodash// any deps named "lodash" & within semver range ^"1.2.3"#lodash@^1.2.3// equivalent to...[name="lodash"]:semver(^1.2.3)// get the hoisted node for a given semver range#lodash@^1.2.3:not(:deduped)// querying deps with a specific version#lodash@2.1.5// equivalent to...[name="lodash"][version="2.1.5"]// has any deps:has(*)// deps with no other deps (ie. "leaf" nodes):empty// manually querying git dependencies[repository^=github:],[repository^=git:],[repository^=https://github.com],[repository^=http://github.com],[repository^=https://github.com],[repository^=+git:...]// querying for all git dependencies:type(git)// get production dependencies that aren't also dev deps.prod:not(.dev)// get dependencies with specific licenses[license=MIT], [license=ISC]// find all packages that have @ruyadorno as a contributor:attr(contributors, [email=ruyadorno@github.com])
示例响应输出
¥Example Response Output
-
返回一个依赖对象数组,其中可以包含同一包的多个副本,这些副本可能已链接或已删除,也可能未链接或数据去重
¥an array of dependency objects is returned which can contain multiple copies of the same package which may or may not have been linked or deduped
[{"name": "","version": "","description": "","homepage": "","bugs": {},"author": {},"license": {},"funding": {},"files": [],"main": "","browser": "","bin": {},"man": [],"directories": {},"repository": {},"scripts": {},"config": {},"dependencies": {},"devDependencies": {},"optionalDependencies": {},"bundledDependencies": {},"peerDependencies": {},"peerDependenciesMeta": {},"engines": {},"os": [],"cpu": [],"workspaces": {},"keywords": [],...},...
期待一定数量的结果
¥Expecting a certain number of results
npm query
的一个常见用途是确保树中某个依赖只有一个版本。对于像依赖 typescript
这样的生态系统来说,这种情况尤其常见,其中状态分散在两个不同但名称相同的包中会导致错误。你可以在设置中使用 --expect-results
或 --expect-result-count
,以确保如果你的树看起来不像你想要的那样,npm 将退出并带有退出代码。
¥One common use of npm query
is to make sure there is only one version of a certain dependency in your tree. This is especially common for ecosystems like that rely on typescript
where having state split across two different but identically-named packages causes bugs. You can use the --expect-results
or --expect-result-count
in your setup to ensure that npm will exit with an exit code if your tree doesn't look like you want it to.
$ npm query '#react' --expect-result-count=1
也许你想快速检查是否有任何可以更新的生产依赖:
¥Perhaps you want to quickly check if there are any production dependencies that could be updated:
$ npm query ':root>:outdated(in-range).prod' --no-expect-results
仅包锁定模式
¥Package lock only mode
如果启用了 package-lock-only,则仅加载包锁(或收缩封装)中的信息。这意味着依赖的 package.json 文件中的信息将不会包含在结果集中(例如描述、主页、引擎)。
¥If package-lock-only is enabled, only the information in the package lock (or shrinkwrap) is loaded. This means that information from the package.json files of your dependencies will not be included in the result set (e.g. description, homepage, engines).
配置
¥Configuration
global
-
默认值:false
¥Default: false
-
类型:布尔值
¥Type: Boolean
在 "global" 模式下运行,以便将包安装到 prefix
文件夹而不是当前工作目录。有关行为差异的更多信息,请参见 文件夹。
¥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
文件夹,而不是当前工作目录。¥packages are installed into the
{prefix}/lib/node_modules
folder, instead of the current working directory. -
bin 文件链接到
{prefix}/bin
¥bin files are linked to
{prefix}/bin
-
手册页链接到
{prefix}/share/man
¥man pages are linked to
{prefix}/share/man
workspace
-
默认值:
¥Default:
-
类型:字符串(可以设置多次)
¥Type: String (can be set multiple times)
启用在当前项目的已配置工作区的上下文中运行命令,同时通过仅运行此配置选项定义的工作区进行过滤。
¥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:
-
工作区名称
¥Workspace names
-
工作区目录的路径
¥Path to a workspace directory
-
父工作区目录的路径(将导致选择该文件夹中的所有工作区)
¥Path to a parent workspace directory (will result in selecting all workspaces within that folder)
为 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
-
默认值:null
¥Default: null
-
类型:空值或布尔值
¥Type: null or Boolean
设置为 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
配置中指定了一个或多个工作区。¥Commands that operate on the
node_modules
tree (install, update, etc.) will link workspaces into thenode_modules
folder. - Commands that do other things (test, exec, publish, etc.) will operate on the root project, unless one or more workspaces are specified in theworkspace
config.
此值不会导出到子进程的环境中。
¥This value is not exported to the environment for child processes.
include-workspace-root
-
默认值:false
¥Default: false
-
类型:布尔值
¥Type: Boolean
为命令启用工作区时包括工作区根。
¥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.
package-lock-only
-
默认值:false
¥Default: false
-
类型:布尔值
¥Type: Boolean
如果设置为 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
.
expect-results
-
默认值:null
¥Default: null
-
类型:空值或布尔值
¥Type: null or Boolean
告诉 npm 是否期望该命令的结果。可以为 true(期望一些结果)或 false(期望没有结果)。
¥Tells npm whether or not to expect results from the command. Can be either true (expect some results) or false (expect no results).
此配置不能用于:expect-result-count
¥This config can not be used with: expect-result-count
expect-result-count
-
默认值:null
¥Default: null
-
类型:空值或数字
¥Type: null or Number
告知期望从命令中得到特定数量的结果。
¥Tells to expect a specific number of results from the command.
此配置不能用于:expect-results
¥This config can not be used with: expect-results
也可以看看
¥See Also