`pnpm pkg fix` left `repository` as written. It now follows the
fixRepositoryField step of `npm pkg fix`: a non-empty string becomes
{ "type": "git", "url": <string> }, and a url that hosted-git-info
recognizes becomes https() for a shortcut and the form of its own
protocol otherwise. Unrecognized urls, other keys, and a shortcut with
no owner stay as written. The legacy `repositories` array step is not
ported.
The TypeScript CLI uses the hosted-git-info fork the git resolver
already depends on. The Rust CLI adds HostedGit::normalized_repository_url,
because HostedGit's Display renders the git and http forms differently
from hosted-git-info's toString(). Rust does not recognize gist urls, so
a `gist:` repository keeps its url there, while the TypeScript CLI
normalizes it like npm.
Closes pnpm/pnpm#16980
84 lines
2.4 KiB
Markdown
84 lines
2.4 KiB
Markdown
---
|
|
id: pkg
|
|
title: pnpm pkg
|
|
---
|
|
|
|
Added in: v11.3.0
|
|
|
|
Manages the contents of `package.json` from the command line.
|
|
|
|
```sh
|
|
pnpm pkg get [<key> [<key> ...]]
|
|
pnpm pkg set <key>=<value> [<key>=<value> ...]
|
|
pnpm pkg delete <key> [<key> ...]
|
|
pnpm pkg fix
|
|
```
|
|
|
|
Nested fields are addressed with dot-separated paths (e.g. `scripts.test`, `repository.url`).
|
|
|
|
## Commands
|
|
|
|
### get
|
|
|
|
Retrieves a value from `package.json`. With no arguments, prints the full manifest. With one or more keys, prints the requested fields.
|
|
|
|
```sh
|
|
pnpm pkg get name
|
|
pnpm pkg get name version
|
|
pnpm pkg get scripts.test
|
|
```
|
|
|
|
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
|
|
|
|
Sets one or more values in `package.json`. Each argument has the `key=value` form.
|
|
|
|
```sh
|
|
pnpm pkg set name=my-package
|
|
pnpm pkg set scripts.build="tsc -p ."
|
|
pnpm pkg set 'keywords[0]'=cli
|
|
```
|
|
|
|
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):
|
|
|
|
```sh
|
|
pnpm pkg set private=true --json
|
|
pnpm pkg set 'engines={"node":">=22"}' --json
|
|
```
|
|
|
|
### delete
|
|
|
|
Removes one or more keys from `package.json`.
|
|
|
|
```sh
|
|
pnpm pkg delete scripts.test
|
|
pnpm pkg delete keywords
|
|
```
|
|
|
|
### fix
|
|
|
|
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). It also converts a `repository` string to the object form and normalizes a GitHub, GitLab, or Bitbucket URL the way `npm pkg fix` does.
|
|
|
|
```sh
|
|
pnpm pkg fix
|
|
```
|
|
|
|
## Options
|
|
|
|
### --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
|
|
|
|
Runs the subcommand on every workspace project, or on every project selected by a `--filter`.
|
|
|
|
```sh
|
|
pnpm -r pkg get name
|
|
pnpm -r pkg set version=1.0.0
|
|
pnpm --filter "./packages/*" pkg get name
|
|
```
|
|
|
|
`pnpm -r pkg get` returns a JSON object keyed by package name. A project is keyed by its directory, relative to the workspace root, when it has no name, when another selected project has the same name, or when its name matches the directory key of another project reported that way. `set`, `delete`, and `fix` apply to each matched project.
|