选择命令行版本:
🌐 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> ...]npm pkg fix
🌐 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。
例如,为了检索当前包的名称,你可以运行:
npm pkg get name
也可以一次检索多个值:
npm pkg get name version
你可以通过用句点分隔来查看子字段。要获取测试值 script 的值,你可以运行以下命令:
npm pkg get scripts.test
对于数组类型的字段,请求非数字字段将返回列表中对象的所有值。例如,要获取一个软件包的所有贡献者邮箱,你可以运行如下操作:
npm pkg get contributors.email
你还可以在方括号中使用数字索引来专门选择数组字段中的某个项目。要获取列表中第一个贡献者的电子邮件地址,你可以运行:
npm pkg get contributors[0].email
对于复杂的字段,你也可以在方括号中命名一个属性,以专门选择子字段。这在处理 exports 对象时尤其有用:
npm pkg get "exports[.].require"
npm pkg set <field>=<value>
根据 field 的值在你的 package.json 中设置一个 value。在保存到你的 package.json 文件时,会使用与 npm install 和其他涉及 package.json 文件的命令相同的一套规则,确保遵循现有的缩进,并可能在将值保存到文件之前进行一些验证。
用于从你的包中获取值的相同语法也可以用来定义新属性或覆盖现有属性,下面是一些关于如何使用点分隔语法编辑你的 package.json 文件的示例。
在你的 package.json 中定义一个名为 mynewcommand 的新二进制文件,该文件指向 cli.js 文件:
npm pkg set bin.mynewcommand=cli.js
一次设置多个字段也是可能的:
npm pkg set description='Awesome package' engines.node='>=10'
也可以添加到数组值,例如添加新的贡献者条目:
npm pkg set contributors[0].name='Foo' contributors[0].email='foo@bar.ca'
你还可以使用特殊的空括号表示法将项目附加到数组的末尾:
npm pkg set contributors[].name='Foo' contributors[].name='Bar'
也可以在将值保存到你的 package.json 文件之前将其解析为 JSON,例如为了设置一个 "private": true 属性:
npm pkg set private=true --json
它还可以将值保存为数字:
npm pkg set tap.timeout=60 --json
npm pkg delete <key>
从你的 package.json 中删除一个 key
用于从你的包中设置值的相同语法也可以用来删除现有的值。例如,要删除名为 build 的脚本:
npm pkg delete scripts.build
npm pkg fix
自动更正你的 package.json 中的常见错误。npm 在 publish 期间已经执行了此操作,这会导致你的 package.json 文件内容与 npm 安装期间使用的清单之间出现微妙的(大多无害的)差异。
🌐 Workspaces support
你可以使用 workspace 或 workspaces 配置选项,在你配置的工作区中设置、获取或删除条目。
🌐 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删除了针对不幸的副作用、常见错误、不必要的性能下降和恶意输入的各种保护。
🌐 Removes various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input.
npm version 命令在不干净的 git 仓库上运行。npm cache clean 删除缓存文件夹。engines 声明且需要不同版本 npm 的包。engines 声明且需要不同版本的 node 的软件包,即使启用了 --engine-strict。npm audit fix 安装超出你指定依赖范围的模块(包括 SemVer 主版本更改)。npm init 期间隐式设置 --yes。npm pkg 中的现有值如果你对自己想要做什么没有明确的想法,强烈建议你不要使用此选项!
🌐 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是否输出 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.
🌐 See Also