npm-pkg

管理你的 package.json

选择命令行版本:

概要

¥Synopsis

npm pkg set <key>=<value> [<key>=<value> ...]
npm pkg get [<key> [<key> ...]]
npm pkg delete <key> [<key> ...]
npm pkg set [<array>[<index>].<key>=<value> ...]
npm pkg set [<array>[].<key>=<value> ...]

描述

¥Description

自动管理 package.json 文件的命令。npm pkg 提供 3 个不同的子命令,允许你修改或检索 package.json 中给定对象键的值。

¥A command that automates the management of package.json files. npm pkg provide 3 different sub commands that allow you to modify or retrieve values for given object keys in your package.json.

检索和设置字段的语法是在 package.json 中找到的嵌套对象属性的点分隔表示,它与 npm view 中用于从注册表清单中检索信息的表示法相同,你可以在下面找到有关如何使用它的更多示例.

¥The syntax to retrieve and set fields is a dot separated representation of the nested object properties to be found within your package.json, it's the same notation used in npm view to retrieve information from the registry manifest, below you can find more examples on how to use it.

返回值始终为 json 格式。

¥Returned values are always in json format.

  • npm pkg get <field>

    检索在 package.json 文件中定义的值 key

    ¥Retrieves a value key, defined in your package.json file.

    例如,为了检索当前包的名称,你可以运行:

    ¥For example, in order to retrieve the name of the current package, you can run:

    npm pkg get name

    也可以一次检索多个值:

    ¥It's also possible to retrieve multiple values at once:

    npm pkg get name version

    你可以通过用句点分隔子字段来查看子字段。要检索测试 script 值的值,你将运行以下命令:

    ¥You can view child fields by separating them with a period. To retrieve the value of a test script value, you would run the following command:

    npm pkg get scripts.test

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

    ¥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 emails for a package, you would run:

    npm pkg get 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 pkg get contributors[0].email

    对于复杂字段,你还可以在方括号中命名属性以专门选择子字段。这对导出对象特别有用:

    ¥For complex fields you can also name a property in square brackets to specifically select a child field. This is especially helpful with the exports object:

    npm pkg get "exports[.].require"
  • npm pkg set <field>=<value>

    根据 field 值在 package.json 中设置 value。保存到 package.json 文件时,使用在 npm install 和其他涉及 package.json 文件的 cli 命令期间使用的相同规则集,确保尊重现有缩进并可能在将值保存到文件之前应用一些验证。

    ¥Sets a value in your package.json based on the field value. When saving to your package.json file the same set of rules used during npm install and other cli commands that touches the package.json file are used, making sure to respect the existing indentation and possibly applying some validation prior to saving values to the file.

    用于从包中检索值的相同语法也可用于定义新属性或覆盖现有属性,以下是如何使用点分隔语法编辑 package.json 文件的一些示例。

    ¥The same syntax used to retrieve values from your package can also be used to define new properties or overriding existing ones, below are some examples of how the dot separated syntax can be used to edit your package.json file.

    在你的 package.json 中定义一个名为 mynewcommand 的新 bin,它指向一个文件 cli.js

    ¥Defining a new bin named mynewcommand in your package.json that points to a file cli.js:

    npm pkg set bin.mynewcommand=cli.js

    一次设置多个字段也是可能的:

    ¥Setting multiple fields at once is also possible:

    npm pkg set description='Awesome package' engines.node='>=10'

    也可以添加到数组值,例如添加新的贡献者条目:

    ¥It's also possible to add to array values, for example to add a new contributor entry:

    npm pkg set contributors[0].name='Foo' contributors[0].email='foo@bar.ca'

    你还可以使用特殊的空括号表示法将项目附加到数组的末尾:

    ¥You may also append items to the end of an array using the special empty bracket notation:

    npm pkg set contributors[].name='Foo' contributors[].name='Bar'

    在将值保存到 package.json 文件之前,也可以将值解析为 json,例如为了设置 "private": true 属性:

    ¥It's also possible to parse values as json prior to saving them to your package.json file, for example in order to set a "private": true property:

    npm pkg set private=true --json

    它还可以将值保存为数字:

    ¥It also enables saving values as numbers:

    npm pkg set tap.timeout=60 --json
  • npm pkg delete <key>

    从你的 package.json 中删除一个 key

    ¥Deletes a key from your package.json

    用于从包中设置值的相同语法也可用于删除现有值。例如,为了删除名为 build 的脚本:

    ¥The same syntax used to set values from your package can also be used to remove existing ones. For example, in order to remove a script named build:

    npm pkg delete scripts.build

工作区支持

¥Workspaces support

你可以使用 workspaceworkspaces 配置选项在已配置的工作区中设置/获取/删除项目。

¥You can set/get/delete items across your configured workspaces by using the workspace or workspaces config options.

例如,在项目的所有已配置工作区中设置 funding 值:

¥For example, setting a funding value across all configured workspaces of a project:

npm pkg set funding=https://example.com --ws

当使用 npm pkg get 从你配置的工作区中检索信息时,返回的结果将采用 json 格式,其中顶层键是每个工作区的名称,这些键的值将是从每个配置的工作区返回的结果值,例如:

¥When using npm pkg get to retrieve info from your configured workspaces, the returned result will be in a json format in which top level keys are the names of each workspace, the values of these keys will be the result values returned from each of the configured workspaces, e.g:

npm pkg get name version --ws
{
"a": {
"name": "a",
"version": "1.0.0"
},
"b": {
"name": "b",
"version": "1.0.0"
}
}

配置

¥Configuration

force

  • 默认值:false

    ¥Default: false

  • 类型:布尔值

    ¥Type: Boolean

删除了针对不幸的副作用、常见错误、不必要的性能下降和恶意输入的各种保护。

¥Removes various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input.

  • 允许在全局安装中破坏非 npm 文件。

    ¥Allow clobbering non-npm files in global installs.

  • 允许 npm version 命令在不干净的 git 存储库上工作。

    ¥Allow the npm version command to work on an unclean git repository.

  • 允许使用 npm cache clean 删除缓存文件夹。

    ¥Allow deleting the cache folder with npm cache clean.

  • 允许安装具有 engines 声明需要不同版本的 npm 的包。

    ¥Allow installing packages that have an engines declaration requiring a different version of npm.

  • 允许安装具有 engines 声明需要不同版本 node 的包,即使启用了 --engine-strict

    ¥Allow installing packages that have an engines declaration requiring a different version of node, even if --engine-strict is enabled.

  • 允许 npm audit fix 安装超出你声明的依赖范围的模块(包括 SemVer 的主要更改)。

    ¥Allow npm audit fix to install modules outside your stated dependency range (including SemVer-major changes).

  • 允许取消发布已发布包的所有版本。

    ¥Allow unpublishing all versions of a published package.

  • 允许在根项目中安装冲突的 peerDependencies。

    ¥Allow conflicting peerDependencies to be installed in the root project.

  • npm init 时隐式设置 --yes

    ¥Implicitly set --yes during npm init.

  • 允许破坏 npm pkg 中的现有值

    ¥Allow clobbering existing values in npm pkg

  • 允许取消发布整个包(不仅仅是单个版本)。

    ¥Allow unpublishing of entire packages (not just a single version).

如果你对自己想要做什么没有明确的想法,强烈建议你不要使用此选项!

¥If you don't have a clear idea of what you want to do, it is strongly recommended that you do not use this option!

json

  • 默认值:false

    ¥Default: false

  • 类型:布尔值

    ¥Type: Boolean

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

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

  • npm pkg set 中,它可以使用 JSON.parse() 解析集合值,然后再将它们保存到你的 package.json

    ¥In npm pkg set it enables parsing set values with JSON.parse() before saving them to your package.json.

并非所有 npm 命令都支持。

¥Not supported by all npm commands.

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 the node_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 the workspace config.

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

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

也可以看看

¥See Also

npm 中文网 - 粤ICP备13048890号