Skip to content

Commit e5dc9fe

Browse files
committed
CLI documentation update from CI
1 parent bd430ed commit e5dc9fe

File tree

14 files changed

+230
-42
lines changed

14 files changed

+230
-42
lines changed

cli-cache.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"v8": "aa8fff11cdab94fff1a2160ee5241f5f4632e96b",
33
"v9": "64763a341e7aa5b456e696f956759bf9b3440dc1",
44
"v10": "6755ca2d5301b079f9e2581fc72ba2a612171364",
5-
"v11": "54929cef8e26a4698234e5d2499a43b746569b12"
5+
"v11": "4ebb831d93f13cc0b980754bf36abb2982b131f7"
66
}

content/cli/v11/commands/npm-install.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ Even if you never publish your package, you can still get a lot of benefits of u
121121

122122
In most cases, this will install the version of the modules tagged as `latest` on the npm registry.
123123

124+
**Note:** When installing by name without specifying a version or tag, npm prioritizes versions that match the current Node.js version based on the package's `engines` field. If the `latest` tag points to a version incompatible with your current Node.js version, npm will install the newest compatible version instead. To install a specific version regardless of `engines` compatibility, explicitly specify the version or tag: `npm install <name>@latest`.
125+
124126
Example:
125127

126128
```bash
@@ -209,6 +211,15 @@ Even if you never publish your package, you can still get a lot of benefits of u
209211
npm install @myorg/privatepackage@"16 - 17"
210212
```
211213

214+
**Prerelease versions:** By default, version ranges only match stable versions. To include prerelease versions, they must be explicitly specified in the range. Prerelease versions are tied to a specific version triple (major.minor.patch). For example, `^1.2.3-beta.1` will only match prereleases for `1.2.x`, not `1.3.x`. To match all prereleases for a major version, use a range like `^1.0.0-0`, which will include all `1.x.x` prereleases.
215+
216+
Example:
217+
218+
```bash
219+
npm install package@^1.2.3-beta.1 # Matches 1.2.3-beta.1, 1.2.3-beta.2, 1.2.4-beta.1, etc.
220+
npm install package@^1.0.0-0 # Matches all 1.x.x prereleases and stable versions
221+
```
222+
212223
- `npm install <git remote url>`:
213224

214225
Installs the package from the hosted git provider, cloning it with `git`. For a full git remote url, only that URL will be attempted.
@@ -312,6 +323,8 @@ npm install sax@">=0.1.0 <0.2.0" bench supervisor
312323
313324
The `--tag` argument will apply to all of the specified install targets. If a tag with the given name exists, the tagged version is preferred over newer versions.
314325
326+
**Note:** The `--tag` option only affects packages specified on the command line. It does not override version ranges specified in `package.json`. For example, if `package.json` specifies `"foo": "^1.0.0"` and you run `npm install --tag beta`, npm will still install a version matching `^1.0.0` even if the `beta` tag points to a different version. To install a tagged version, specify the package explicitly: `npm install foo@beta`.
327+
315328
The `--dry-run` argument will report in the usual way what the install would have done without actually installing anything.
316329
317330
The `--package-lock-only` argument will only update the `package-lock.json`, instead of checking `node_modules` and downloading dependencies.

content/cli/v11/commands/npm-ls.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Note: to get a "bottoms up" view of why a given package is included in the tree
5252
Positional arguments are `name@version-range` identifiers, which will limit the results to only the paths to the packages named. Note that nested packages will _also_ show the paths to the specified packages. For example, running `npm ls promzard` in npm's source tree will show:
5353

5454
```bash
55-
npm@11.6.4 /path/to/npm
55+
npm@11.7.0 /path/to/npm
5656
└─┬ init-package-json@0.0.4
5757
└── promzard@0.1.5
5858
```

content/cli/v11/commands/npm-token.mdx

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ redirect_from:
4040
```bash
4141
npm token list
4242
npm token revoke <id|token>
43-
npm token create --name=<name> [--token-description=<desc>] [--packages=<pkg1,pkg2>] [--packages-all] [--scopes=<scope1,scope2>] [--orgs=<org1,org2>] [--packages-and-scopes-permission=<read-only|read-write|no-access>] [--orgs-permission=<read-only|read-write|no-access>] [--expires=<days>] [--cidr=<ip-range>] [--bypass-2fa] [--password=<pass>]
43+
npm token create
4444
```
4545

4646
Note: This command is unaware of workspaces.
@@ -49,27 +49,21 @@ Note: This command is unaware of workspaces.
4949

5050
This lets you list, create and revoke authentication tokens.
5151

52-
- `npm token list`: Shows a table of all active authentication tokens. You can request this as JSON with `--json` or tab-separated values with `--parseable`.
52+
#### Listing tokens
5353

54-
```
55-
Read only token npm_1f… with id 7f3134 created 2017-10-21
56-
57-
Publish token npm_af… with id c03241 created 2017-10-02
58-
with IP Whitelist: 192.168.0.1/24
54+
When listing tokens, an abbreviated token will be displayed. For security purposes the full token is not displayed.
5955

60-
Publish token npm_… with id e0cf92 created 2017-10-02
56+
#### Generating tokens
6157

62-
```
58+
When generating tokens, you will be prompted you for your password and, if you have two-factor authentication enabled, an otp.
6359

64-
- `npm token create [--read-only] [--cidr=<cidr-ranges>]`: Create a new authentication token. It can be `--read-only`, or accept a list of [CIDR](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing) ranges with which to limit use of this token. This will prompt you for your password, and, if you have two-factor authentication enabled, an otp.
60+
Please refer to the [docs website](https://docs.npmjs.com/creating-and-viewing-access-tokens) for more information on generating tokens for CI/CD.
6561

66-
Currently, the cli cannot generate automation tokens. Please refer to the [docs website](https://docs.npmjs.com/creating-and-viewing-access-tokens) for more information on generating automation tokens.
62+
#### Revoking tokens
6763

68-
```
69-
Created publish token a73c9572-f1b9-8983-983d-ba3ac3cc913d
70-
```
64+
When revoking a token, you can use the full token (e.g. what you get back from `npm token create`, or as can be found in an `.npmrc` file), or a truncated id. If the given truncated id is not distinct enough to differentiate between multiple existing tokens, you will need to use enough of the id to allow npm to distinguish between them. Full token ids can be found on the [npm website](https://www.npmjs.com), or in the `--parseable` or `--json` output of `npm token list`. This command will NOT accept the truncated token found in the normal `npm token list` output.
7165

72-
- `npm token revoke <token|id>`: Immediately removes an authentication token from the registry. You will no longer be able to use it. This can accept both complete tokens (such as those you get back from `npm token create`, and those found in your `.npmrc`), and ids as seen in the parseable or json output of `npm token list`. This will NOT accept the truncated token found in the normal `npm token list` output.
66+
A revoked token will immediately be removed from the registry and you will no longer be able to use it.
7367

7468
### Configuration
7569

@@ -99,7 +93,7 @@ When creating a Granular Access Token with `npm token create`, this sets the exp
9993
- Default:
10094
- Type: null or String (can be set multiple times)
10195

102-
When creating a Granular Access Token with `npm token create`, this limits the token access to specific packages. Provide a comma-separated list of package names.
96+
When creating a Granular Access Token with `npm token create`, this limits the token access to specific packages.
10397

10498
#### `packages-all`
10599

@@ -113,14 +107,14 @@ When creating a Granular Access Token with `npm token create`, grants the token
113107
- Default: null
114108
- Type: null or String (can be set multiple times)
115109

116-
When creating a Granular Access Token with `npm token create`, this limits the token access to specific scopes. Provide a comma-separated list of scope names (with or without @ prefix).
110+
When creating a Granular Access Token with `npm token create`, this limits the token access to specific scopes. Provide a scope name (with or without @ prefix).
117111

118112
#### `orgs`
119113

120114
- Default: null
121115
- Type: null or String (can be set multiple times)
122116

123-
When creating a Granular Access Token with `npm token create`, this limits the token access to specific organizations. Provide a comma-separated list of organization names.
117+
When creating a Granular Access Token with `npm token create`, this limits the token access to specific organizations.
124118

125119
#### `packages-and-scopes-permission`
126120

content/cli/v11/commands/npm-version.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,12 @@ Run this in a package directory to bump the version and write the new data back
167167

168168
The `newversion` argument should be a valid semver string, a valid second argument to [semver.inc](https://github.com/npm/node-semver#functions) (one of `patch`, `minor`, `major`, `prepatch`, `preminor`, `premajor`, `prerelease`), or `from-git`. In the second case, the existing version will be incremented by 1 in the specified field. `from-git` will try to read the latest git tag, and use that as the new npm version.
169169

170+
**Note:** If the current version is a prerelease version, `patch` will simply remove the prerelease suffix without incrementing the patch version number. For example, `1.2.0-5` becomes `1.2.0` with `npm version patch`, not `1.2.1`.
171+
170172
If run in a git repo, it will also create a version commit and tag. This behavior is controlled by `git-tag-version` (see below), and can be disabled on the command line by running `npm --no-git-tag-version version`. It will fail if the working directory is not clean, unless the `-f` or `--force` flag is set.
171173

174+
**Note:** Git integration requires a reasonably recent version of git (2.0.0 or later is recommended). If you encounter issues with git commands, ensure your git installation is up to date.
175+
172176
If supplied with `-m` or [`--message` config](/cli/v11/using-npm/config#message) option, npm will use it as a commit message when creating a version commit. If the `message` config contains `%s` then that will be replaced with the resulting version number. For example:
173177

174178
```bash

content/cli/v11/commands/npm-view.mdx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ If the field value you are querying for is a property of an object, you should r
9797
npm view express time'[4.8.0]'
9898
```
9999

100+
Note: When accessing object properties that contain special characters or numeric keys, you need to use quotes around the key name. For example, to get the publish time of a specific version:
101+
102+
```bash
103+
npm view express "time[4.17.1]"
104+
```
105+
106+
Without quotes, the shell may interpret the square brackets as glob patterns, causing the command to fail. You can also access the time field for a specific version by specifying the version in the package descriptor:
107+
108+
```bash
109+
npm view express@4.17.1 time
110+
```
111+
112+
This will return all version-time pairs, but the context will be for that specific version.
113+
100114
Multiple fields may be specified, and will be printed one after another. For example, to get all the contributor names and email addresses, you can do this:
101115

102116
```bash
@@ -121,6 +135,56 @@ To show the `connect` package version history, you can do this:
121135
npm view connect versions
122136
```
123137

138+
### Field Access Patterns
139+
140+
The `npm view` command supports different ways to access nested fields and array elements in package metadata. Understanding these patterns makes it easier to extract specific information.
141+
142+
#### Nested Object Fields
143+
144+
Use dot notation to access nested object fields:
145+
146+
```bash
147+
# Access nested properties
148+
npm view npm repository.url
149+
npm view express bugs.url
150+
```
151+
152+
#### Array Element Access
153+
154+
For arrays, use numeric indices in square brackets to access specific elements:
155+
156+
```bash
157+
# Get the first contributor's email
158+
npm view express contributors[0].email
159+
160+
# Get the second maintainer's name
161+
npm view express maintainers[1].name
162+
```
163+
164+
#### Object Property Access
165+
166+
For object properties (like accessing specific versions in the `time` field), use bracket notation with the property name in quotes:
167+
168+
```bash
169+
# Get publish time for a specific version
170+
npm view express "time[4.17.1]"
171+
172+
# Get dist-tags
173+
npm view express "dist-tags.latest"
174+
```
175+
176+
#### Extracting Fields from Arrays
177+
178+
Request a non-numeric field on an array to get all values from objects in the list:
179+
180+
```bash
181+
# Get all contributor emails
182+
npm view express contributors.email
183+
184+
# Get all contributor names
185+
npm view express contributors.name
186+
```
187+
124188
### Configuration
125189

126190
#### `json`

content/cli/v11/commands/npm.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Note: This command is unaware of workspaces.
3131

3232
### Version
3333

34-
11.6.4
34+
11.7.0
3535

3636
### Description
3737

content/cli/v11/commands/npx.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ npx --package=foo -c '<cmd> [args...]'
3434

3535
This command allows you to run an arbitrary command from an npm package (either one installed locally, or fetched remotely), in a similar context as running it via `npm run`.
3636

37+
Run this command to execute a package's binary. Any options and arguments after the package name are passed directly to the executed command, not to npx itself. For example, `npx create-react-app my-app --template typescript` will pass `my-app` and `--template typescript` to the `create-react-app` command. To see what options a specific package accepts, consult that package's documentation (e.g., at npmjs.com or in its repository).
38+
3739
Whatever packages are specified by the `--package` option will be provided in the `PATH` of the executed command, along with any locally installed package executables. The `--package` option may be specified multiple times, to execute the supplied command in an environment where all specified packages are available.
3840

3941
If any requested packages are not present in the local project dependencies, then they are installed to a folder in the npm cache, which is added to the `PATH` environment variable in the executed process. A prompt is printed (which can be suppressed by providing either `--yes` or `--no`).

content/cli/v11/configuring-npm/package-json.mdx

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ For most modules, it makes the most sense to have a main script and often not mu
325325

326326
If `main` is not set, it defaults to `index.js` in the package's root folder.
327327

328+
### type
329+
330+
The `type` field defines how Node.js should interpret `.js` files in your package. This field is not used by npm.
331+
332+
See the [Node.js documentation on the type field](https://nodejs.org/api/packages.html#type) for more information.
333+
328334
### browser
329335

330336
If your module is meant to be used client-side the browser field should be used instead of the main field. This is helpful to hint users that it might rely on primitives that aren't available in Node.js modules. (e.g. `window`)
@@ -470,6 +476,19 @@ For GitHub, GitHub gist, Bitbucket, or GitLab repositories you can use the same
470476
}
471477
```
472478

479+
**Note on normalization:** When you publish a package, npm normalizes the `repository` field to the full object format with a `url` property. If you use a shorthand format (like `"npm/example"`), you'll see a warning during `npm publish` indicating that the field was auto-corrected. While the shorthand format currently works, it's recommended to use the full object format in your `package.json` to avoid warnings and ensure future compatibility:
480+
481+
```json
482+
{
483+
"repository": {
484+
"type": "git",
485+
"url": "git+https://github.com/npm/example.git"
486+
}
487+
}
488+
```
489+
490+
You can run `npm pkg fix` to automatically convert shorthand formats to the normalized object format.
491+
473492
If the `package.json` for your package is not in the root directory (for example if it is part of a monorepo), you can specify the directory in which it lives:
474493

475494
```json
@@ -488,6 +507,20 @@ The "scripts" property is a dictionary containing script commands that are run a
488507

489508
See [`scripts`](/cli/v11/using-npm/scripts) to find out more about writing package scripts.
490509

510+
### gypfile
511+
512+
If you have a binding.gyp file in the root of your package and you have not defined your own `install` or `preinstall` scripts, npm will default to building your module using node-gyp.
513+
514+
To prevent npm from automatically building your module with node-gyp, set `gypfile` to `false`:
515+
516+
```json
517+
{
518+
"gypfile": false
519+
}
520+
```
521+
522+
This is useful for packages that include native addons but want to handle the build process differently, or packages that have a binding.gyp file but should not be built as a native addon.
523+
491524
### config
492525

493526
A "config" object can be used to set configuration parameters used in package scripts that persist across upgrades. For instance, if a package had the following:
@@ -853,6 +886,52 @@ You may not set an override for a package that you directly depend on unless bot
853886
}
854887
```
855888
889+
#### Replacing a dependency with a fork
890+
891+
You can replace a package with a different package or fork using several methods:
892+
893+
**Using the `npm:` prefix to replace with a different package name:**
894+
895+
```json
896+
{
897+
"overrides": {
898+
"package-name": "npm:@scope/forked-package@1.0.0"
899+
}
900+
}
901+
```
902+
903+
**Using a GitHub repository (supports branches, tags, or commit hashes):**
904+
905+
```json
906+
{
907+
"overrides": {
908+
"package-name": "github:username/repo#branch-name"
909+
}
910+
}
911+
```
912+
913+
**Using a local file path:**
914+
915+
```json
916+
{
917+
"overrides": {
918+
"package-name": "file:../local-fork"
919+
}
920+
}
921+
```
922+
923+
These replacement methods work for both top-level overrides and nested overrides. For example, to replace a transitive dependency with a fork:
924+
925+
```json
926+
{
927+
"overrides": {
928+
"parent-package": {
929+
"vulnerable-dep": "github:username/patched-fork#v2.0.1"
930+
}
931+
}
932+
}
933+
```
934+
856935
### engines
857936
858937
You can specify the version of node that your stuff works on:

content/cli/v11/configuring-npm/package-lock-json.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,16 @@ npm v7 ignores this section entirely if a `packages` section is present, but doe
136136

137137
Dependency objects have the following fields:
138138

139-
- version: a specifier that varies depending on the nature of the package, and is usable in fetching a new copy of it.
139+
- version: a specifier that varies depending on the nature of the package, and is usable in fetching a new copy of it. Note that for peer dependencies that are not installed, or optional dependencies that are not installed, this field may be omitted.
140140
- bundled dependencies: Regardless of source, this is a version number that is purely for informational purposes.
141141
- registry sources: This is a version number. (eg, `1.2.3`)
142142
- git sources: This is a git specifier with resolved committish. (eg, `git+https://example.com/foo/bar#115311855adb0789a0466714ed48a1499ffea97e`)
143143
- http tarball sources: This is the URL of the tarball. (eg, `https://example.com/example-1.3.0.tgz`)
144144
- local tarball sources: This is the file URL of the tarball. (eg `file:///opt/storage/example-1.3.0.tgz`)
145145
- local link sources: This is the file URL of the link. (eg `file:libs/our-module`)
146146

147+
**Note:** The `version` field may be omitted for certain types of dependencies, such as optional peer dependencies that are not installed. In these cases, only metadata fields like `dev`, `optional`, and `peer` will be present.
148+
147149
- integrity: A `sha512` or `sha1` [Standard Subresource Integrity](https://w3c.github.io/webappsec/specs/subresourceintegrity/) string for the artifact that was unpacked in this location. For git dependencies, this is the commit sha.
148150

149151
- resolved: For registry sources this is path of the tarball relative to the registry URL. If the tarball URL isn't on the same server as the registry URL then this is a complete URL. `registry.npmjs.org` is a magic value meaning "the currently configured registry".

0 commit comments

Comments
 (0)