pnpm pkg
新增于:v11.3.0
🌐 Added in: v11.3.0
从命令行管理 package.json 的内容。
🌐 Manages the contents of package.json from the command line.
pnpm pkg get [<key> [<key> ...]]
pnpm pkg set <key>=<value> [<key>=<value> ...]
pnpm pkg delete <key> [<key> ...]
pnpm pkg fix
嵌套字段使用点分路径来表示(例如 scripts.test、repository.url)。
🌐 Nested fields are addressed with dot-separated paths (e.g. scripts.test, repository.url).
命令
🌐 Commands
get
从 package.json 检索一个值。没有参数时,打印完整的清单。有一个或多个键时,打印请求的字段。
🌐 Retrieves a value from package.json. With no arguments, prints the full manifest. With one or more keys, prints the requested fields.
pnpm pkg get name
pnpm pkg get name version
pnpm pkg get scripts.test
当只请求一个键且它解析为字符串时,将打印原始值;否则将对值进行 JSON 编码。传递 --json 可始终打印 JSON。
🌐 When only one key is requested and it resolves to a string, the raw value is printed; otherwise the value is JSON-encoded. Pass --json to always print JSON.
set
在 package.json 中设置一个或多个值。每个参数具有 key=value 形式。
🌐 Sets one or more values in package.json. Each argument has the key=value form.
pnpm pkg set name=my-package
pnpm pkg set scripts.build="tsc -p ."
pnpm pkg set 'keywords[0]'=cli
默认情况下,值以字符串形式存储。在存储之前传递 --json 以将值解析为 JSON(对于布尔值、数字、数组和对象很有用):
🌐 By default the value is stored as a string. Pass --json to parse the value as JSON before storing it (useful for booleans, numbers, arrays, and objects):
pnpm pkg set private=true --json
pnpm pkg set 'engines={"node":">=22"}' --json
delete
从 package.json 中删除一个或多个键。
🌐 Removes one or more keys from package.json.
pnpm pkg delete scripts.test
pnpm pkg delete keywords
fix
自动更正 package.json 中的常见错误(例如,移除非字符串的 name 或 version,删除值不是对象的依赖 / scripts 块,删除既不是字符串也不是对象的 bin 字段)。
🌐 Auto-corrects common errors in package.json (e.g. removes a non-string name or version, drops dependency / scripts blocks whose values aren't objects, drops a bin field that's neither a string nor an object).
pnpm pkg fix
选项
🌐 Options
--json
在设置时,在写入之前将每个 value 解析为 JSON。获取单个键时,返回 JSON 编码的形式而不是原始值。
🌐 When setting, parses each value as JSON before writing. When getting a single key, returns the JSON-encoded form instead of the raw value.
--recursive, -r
在每个工作区项目上运行子命令,或在每个由 --filter 选中的项目上运行。
🌐 Runs the subcommand on every workspace project, or on every project selected by a --filter.
pnpm -r pkg get name
pnpm -r pkg set version=1.0.0
pnpm --filter "./packages/*" pkg get name
pnpm -r pkg get 返回一个以包名为键的 JSON 对象;set、delete 和 fix 适用于每个匹配的项目。