选择命令行版本:
🌐 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.
🌐 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
[{"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 锁定文件(或 shrinkwrap)中的信息。这意味着依赖的 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以“全局”模式运行,因此软件包会安装到 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 文件夹中,而不是当前工作目录。{prefix}/bin 关联{prefix}/share/manworkspace启用在当前项目的已配置工作区的上下文中运行命令,同时通过仅运行此配置选项定义的工作区进行过滤。
🌐 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.
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.
expect-results告诉 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 cannot be used with: expect-result-count
expect-result-count告知期望从命令中得到特定数量的结果。
🌐 Tells to expect a specific number of results from the command.
此配置不能与:expect-results 一起使用
🌐 This config cannot be used with: expect-results
before如果传递给 npm install,将重建 npm 树,使得只安装在指定日期当日或之前可用的版本。如果当前依赖集没有可用版本,该命令将报错。
🌐 If passed to npm install, will rebuild the npm tree such that only versions that were available on or before the given date are installed. If there are no versions available for the current set of dependencies, the command will error.
如果请求的版本是 dist-tag,且给定的标签未通过 --before 过滤器,则将使用小于或等于该标签的最新版本。例如,foo@latest 可能会安装 foo@1.2,即使 latest 是 2.0。
🌐 If the requested version is a dist-tag and the given tag does not pass the --before filter, the most recent version less than or equal to that tag will be used. For example, foo@latest might install foo@1.2 even though latest is 2.0.
如果 before 和 min-release-age 都在同一个来源中设置,before 会生效(一个明确的绝对日期会覆盖相对时间窗口)。在不同来源之间,标准优先级适用(cli > env > project > user > global),因此高优先级的来源总是可以放宽或覆盖低优先级的来源。
🌐 If before and min-release-age are both set in the same source, before wins (an explicit absolute date overrides a relative window). Across sources, the standard precedence applies (cli > env > project > user > global), so a higher-priority source can always relax or override a lower-priority one.
与 min-release-age 一样,当这个截止点阻止了 npm audit fix 本可以安装的修复时,npm 会保留易受攻击的版本,发出警告,并以非零代码退出。
🌐 As with min-release-age, when this cutoff blocks a fix that npm audit fix would install, npm keeps the vulnerable version, warns, and exits with a non-zero code.
名称与 min-release-age-exclude 匹配的包不受此过滤器限制。
🌐 Packages whose names match min-release-age-exclude are exempt from this filter.
min-release-age如果设置,npm 将构建 npm 树,使得只安装在指定天数之前可用的版本。如果当前依赖的版本不可用,该命令将报错。
🌐 If set, npm will build the npm tree such that only versions that were available more than the given number of days ago will be installed. If there are no versions available for the current set of dependencies, the command will error.
这个标志是对 before 的补充,后者接受的是确切日期而不是相对的天数。两者可以共存(例如,当 npm 在内部生成子进程以准备 git: 或 github: 依赖时,你的 .npmrc 中的 min-release-age 会被保留);当两者同时适用时,在单个来源中 before 优先,而在多个来源之间则适用标准优先规则。
🌐 This flag is a complement to before, which accepts an exact date instead of a relative number of days. The two may coexist (e.g. min-release-age in your .npmrc is preserved when npm internally spawns a sub-process with --before while preparing a git: or github: dependency); when both apply, before wins within a single source and across sources the standard precedence rules apply.
当此窗口阻止 npm audit fix 安装补丁版本(因为修复发布得太新)时,npm 会将软件包保持在易受攻击的版本,警告修复被阻止,并以非零代码退出。要安装修复,请将软件包添加到 min-release-age-exclude,或放宽 min-release-age 或 before。
🌐 When this window stops npm audit fix from installing a patched version (because the fix was published too recently), npm keeps the package at its vulnerable version, warns that the fix was blocked, and exits with a non-zero code. To install the fix, add the package to min-release-age-exclude, or relax min-release-age or before.
名称与 min-release-age-exclude 匹配的包不受此过滤器限制。
🌐 Packages whose names match min-release-age-exclude are exempt from this filter.
此值不会导出到子进程的环境中。
🌐 This value is not exported to the environment for child processes.
min-release-age-exclude一个包名称列表或 minimatch 通配符模式,这些包不受 min-release-age(和 before)过滤器的限制。即使设置了发布年龄窗口,匹配的包也总是可以解析到其最新版本。
🌐 A list of package names or minimatch glob patterns that are exempt from the min-release-age (and before) filter. A matching package can always resolve to its newest version, even when a release-age window is set.
例如,对第三方依赖应用发布年龄窗口,同时允许内部维护的软件包立即更新:
🌐 For example, to apply a release-age window to third-party dependencies while letting internally maintained packages update immediately:
min-release-age=7min-release-age-exclude[]=@myorg/*min-release-age-exclude[]=my-internal-pkg
只有指定的软件包是例外;其自身的依赖仍然遵循发布时间政策,除非它们也匹配某个模式。模式是针对软件包名称匹配的,所以 @myorg/* 可以匹配 @myorg/shared-utils。
🌐 Only the named package is exempt; its own dependencies still follow the release-age policy unless they also match a pattern. Patterns match against the package name, so @myorg/* matches @myorg/shared-utils.
排除一个包不会改变它从哪个注册表获取。你应该在公共注册表上拥有自己的私有范围,这样就不会有人发布同名的包。
🌐 Excluding a package does not change which registry it is fetched from. You should own your private scope on the public registry so that nobody else can publish a package with the same name.
此值不会导出到子进程的环境中。
🌐 This value is not exported to the environment for child processes.
🌐 See Also