1 ---
2 title: npm-audit
3 section: 1
4 description: Run a security audit
5 github_repo: npm/cli
6 github_branch: latest
7 github_path: docs/lib/content/commands/npm-audit.md
8 redirect_from:
9 - /cli-commands/audit
10 - /cli-commands/npm-audit
11 - /cli-documentation/audit
12 - /cli-documentation/cli-commands/audit
13 - /cli-documentation/cli-commands/npm-audit
14 - /cli-documentation/commands/audit
15 - /cli-documentation/commands/npm-audit
16 - /cli-documentation/npm-audit
17 - /cli-documentation/v11/audit
18 - /cli-documentation/v11/cli-commands/audit
19 - /cli-documentation/v11/cli-commands/npm-audit
20 - /cli-documentation/v11/commands/audit
21 - /cli-documentation/v11/commands/npm-audit
22 - /cli-documentation/v11/npm-audit
23 - /cli/audit
24 - /cli/cli-commands/audit
25 - /cli/cli-commands/npm-audit
26 - /cli/commands/audit
27 - /cli/commands/npm-audit
28 - /cli/npm-audit
29 - /cli/v11/audit
30 - /cli/v11/cli-commands/audit
31 - /cli/v11/cli-commands/npm-audit
32 - /cli/v11/commands/audit
33 - /cli/v11/npm-audit
34 - /commands/audit
35 - /commands/npm-audit
36 ---
37
38 ### Synopsis
39
40 ```bash
41 npm audit [fix|signatures]
42 ```
43
44 ### Description
45
46 The audit command submits a description of the dependencies configured in your project to your default registry and asks for a report of known vulnerabilities. If any vulnerabilities are found, then the impact and appropriate remediation will be calculated. If the `fix` argument is provided, then remediations will be applied to the package tree.
47
48 The command will exit with a 0 exit code if no vulnerabilities were found.
49
50 Note that some vulnerabilities cannot be fixed automatically and will require manual intervention or review. Also note that since `npm audit fix` runs a full-fledged `npm install` under the hood, all configs that apply to the installer will also apply to `npm install` -- so things like `npm audit fix --package-lock-only` will work as expected.
51
52 By default, the audit command will exit with a non-zero code if any vulnerability is found. It may be useful in CI environments to include the `--audit-level` parameter to specify the minimum vulnerability level that will cause the command to fail. This option does not filter the report output, it simply changes the command's failure threshold.
53
54 ### Package lock
55
56 By default npm requires a package-lock or shrinkwrap in order to run the audit. You can bypass the package lock with `--no-package-lock` but be aware the results may be different with every run, since npm will re-build the dependency tree each time.
57
58 ### Audit Signatures
59
60 To ensure the integrity of packages you download from the public npm registry, or any registry that supports signatures, you can verify the registry signatures of downloaded packages using the npm CLI.
61
62 Registry signatures can be verified using the following `audit` command:
63
64 ```bash
65 $ npm audit signatures
66 ```
67
68 The `audit signatures` command will also verify the provenance attestations of downloaded packages. Because provenance attestations are such a new feature, security features may be added to (or changed in) the attestation format over time. To ensure that you're always able to verify attestation signatures check that you're running the latest version of the npm CLI. Please note this often means updating npm beyond the version that ships with Node.js.
69
70 To include the full sigstore attestation bundles in JSON output, use:
71
72 ```bash
73 $ npm audit signatures --json --include-attestations
74 ```
75
76 This adds a `verified` array to the JSON output containing the attestation bundles (DSSE envelopes, verification material, and transparency log entries) for each verified package.
77
78 The npm CLI supports registry signatures and signing keys provided by any registry if the following conventions are followed:
79
80 1. Signatures are provided in the package's `packument` in each published version within the `dist` object:
81
82 ```json
83 "dist":{
84 "..omitted..": "..omitted..",
85 "signatures": [{
86 "keyid": "SHA256:{{SHA256_PUBLIC_KEY}}",
87 "sig": "a312b9c3cb4a1b693e8ebac5ee1ca9cc01f2661c14391917dcb111517f72370809..."
88 }]
89 }
90 ```
91
92 See this [example](https://registry.npmjs.org/light-cycle/1.4.3) of a signed package from the public npm registry.
93
94 The `sig` is generated using the following template: `${package.name}@${package.version}:${package.dist.integrity}` and the `keyid` has to match one of the public signing keys below.
95
96 2. Public signing keys are provided at `registry-host.tld/-/npm/v1/keys` in the following format:
97
98 ```
99 {
100 "keys": [{
101 "expires": null,
102 "keyid": "SHA256:{{SHA256_PUBLIC_KEY}}",
103 "keytype": "ecdsa-sha2-nistp256",
104 "scheme": "ecdsa-sha2-nistp256",
105 "key": "{{B64_PUBLIC_KEY}}"
106 }]
107 }
108 ```
109
110 Keys response:
111
112 - `expires`: null or a simplified extended [ISO 8601 format](https://en.wikipedia.org/wiki/ISO_8601): `YYYY-MM-DDTHH:mm:ss.sssZ`
113 - `keyid`: sha256 fingerprint of the public key
114 - `keytype`: only `ecdsa-sha2-nistp256` is currently supported by the npm CLI
115 - `scheme`: only `ecdsa-sha2-nistp256` is currently supported by the npm CLI
116 - `key`: base64 encoded public key
117
118 See this [example key's response from the public npm registry](https://registry.npmjs.org/-/npm/v1/keys).
119
120 ### Audit Endpoints
121
122 There are two audit endpoints that npm may use to fetch vulnerability information: the `Bulk Advisory` endpoint and the `Quick Audit` endpoint.
123
124 #### Bulk Advisory Endpoint
125
126 As of version 7, npm uses the much faster `Bulk Advisory` endpoint to optimize the speed of calculating audit results.
127
128 npm will generate a JSON payload with the name and list of versions of each package in the tree, and POST it to the default configured registry at the path `/-/npm/v1/security/advisories/bulk`.
129
130 Any packages in the tree that do not have a `version` field in their package.json file will be ignored. If any `--omit` options are specified (either via the [`--omit` config](/cli/v11/using-npm/config#omit), or one of the shorthands such as `--production`, `--only=dev`, and so on), then packages will be omitted from the submitted payload as appropriate.
131
132 If the registry responds with an error, or with an invalid response, then npm will attempt to load advisory data from the `Quick Audit` endpoint.
133
134 The expected result will contain a set of advisory objects for each dependency that matches the advisory range. Each advisory object contains a `name`, `url`, `id`, `severity`, `vulnerable_versions`, and `title`.
135
136 npm then uses these advisory objects to calculate vulnerabilities and meta-vulnerabilities of the dependencies within the tree.
137
138 #### Quick Audit Endpoint
139
140 If the `Bulk Advisory` endpoint returns an error, or invalid data, npm will attempt to load advisory data from the `Quick Audit` endpoint, which is considerably slower in most cases.
141
142 The full package tree as found in `package-lock.json` is submitted, along with the following pieces of additional metadata:
143
144 - `npm_version`
145 - `node_version`
146 - `platform`
147 - `arch`
148 - `node_env`
149
150 All packages in the tree are submitted to the Quick Audit endpoint. Omitted dependency types are skipped when generating the report.
151
152 #### Scrubbing
153
154 Out of an abundance of caution, npm versions 5 and 6 would "scrub" any packages from the submitted report if their name contained a `/` character, so as to avoid leaking the names of potentially private packages or git URLs.
155
156 However, in practice, this resulted in audits often failing to properly detect meta-vulnerabilities, because the tree would appear to be invalid due to missing dependencies, and prevented the detection of vulnerabilities in package trees that used git dependencies or private modules.
157
158 This scrubbing has been removed from npm as of version 7.
159
160 #### Calculating Meta-Vulnerabilities and Remediations
161
162 npm uses the [`@npmcli/metavuln-calculator`](http://npm.im/@npmcli/metavuln-calculator) module to turn a set of security advisories into a set of "vulnerability" objects. A "meta-vulnerability" is a dependency that is vulnerable by virtue of dependence on vulnerable versions of a vulnerable package.
163
164 For example, if the package `foo` is vulnerable in the range `>=1.0.2 <2.0.0`, and the package `bar` depends on `foo@^1.1.0`, then that version of `bar` can only be installed by installing a vulnerable version of `foo`. In this case, `bar` is a "metavulnerability".
165
166 Once metavulnerabilities for a given package are calculated, they are cached in the `~/.npm` folder and only re-evaluated if the advisory range changes, or a new version of the package is published (in which case, the new version is checked for metavulnerable status as well).
167
168 If the chain of metavulnerabilities extends all the way to the root project, and it cannot be updated without changing its dependency ranges, then `npm audit fix` will require the `--force` option to apply the remediation. If remediations do not require changes to the dependency ranges, then all vulnerable packages will be updated to a version that does not have an advisory or metavulnerability posted against it.
169
170 ### Exit Code
171
172 The `npm audit` command will exit with a 0 exit code if no vulnerabilities were found. The `npm audit fix` command will exit with 0 exit code if no vulnerabilities are found _or_ if the remediation is able to successfully fix all vulnerabilities.
173
174 If vulnerabilities were found the exit code will depend on the [`audit-level` config](/cli/v11/using-npm/config#audit-level).
175
176 ### Examples
177
178 Scan your project for vulnerabilities and automatically install any compatible updates to vulnerable dependencies:
179
180 ```bash
181 $ npm audit fix
182 ```
183
184 Run `audit fix` without modifying `node_modules`, but still updating the pkglock:
185
186 ```bash
187 $ npm audit fix --package-lock-only
188 ```
189
190 Skip updating `devDependencies`:
191
192 ```bash
193 $ npm audit fix --only=prod
194 ```
195
196 Have `audit fix` install SemVer-major updates to toplevel dependencies, not just SemVer-compatible ones:
197
198 ```bash
199 $ npm audit fix --force
200 ```
201
202 Do a dry run to get an idea of what `audit fix` will do, and _also_ output install information in JSON format:
203
204 ```bash
205 $ npm audit fix --dry-run --json
206 ```
207
208 Scan your project for vulnerabilities and just show the details, without fixing anything:
209
210 ```bash
211 $ npm audit
212 ```
213
214 Get the detailed audit report in JSON format:
215
216 ```bash
217 $ npm audit --json
218 ```
219
220 Fail an audit only if the results include a vulnerability with a level of moderate or higher:
221
222 ```bash
223 $ npm audit --audit-level=moderate
224 ```
225
226 ### Configuration
227
228 #### `audit-level`
229
230 - Default: null
231 - Type: null, "info", "low", "moderate", "high", "critical", or "none"
232
233 The minimum level of vulnerability for `npm audit` to exit with a non-zero exit code.
234
235 #### `dry-run`
236
237 - Default: false
238 - Type: Boolean
239
240 Indicates that you don't want npm to make any changes and that it should only report what it would have done. This can be passed into any of the commands that modify your local installation, eg, `install`, `update`, `dedupe`, `uninstall`, as well as `pack` and `publish`.
241
242 Note: This is NOT honored by other network related commands, eg `dist-tags`, `owner`, etc.
243
244 #### `force`
245
246 - Default: false
247 - Type: Boolean
248
249 Removes various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input.
250
251 - Allow clobbering non-npm files in global installs.
252 - Allow the `npm version` command to work on an unclean git repository.
253 - Allow deleting the cache folder with `npm cache clean`.
254 - Allow installing packages that have an `engines` declaration requiring a different version of npm.
255 - Allow installing packages that have an `engines` declaration requiring a different version of `node`, even if `--engine-strict` is enabled.
256 - Allow `npm audit fix` to install modules outside your stated dependency range (including SemVer-major changes).
257 - Allow unpublishing all versions of a published package.
258 - Allow conflicting peerDependencies to be installed in the root project.
259 - Implicitly set `--yes` during `npm init`.
260 - Allow clobbering existing values in `npm pkg`
261 - Allow unpublishing of entire packages (not just a single version).
262
263 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!
264
265 #### `json`
266
267 - Default: false
268 - Type: Boolean
269
270 Whether or not to output JSON data, rather than the normal output.
271
272 - In `npm pkg set` it enables parsing set values with JSON.parse() before saving them to your `package.json`.
273
274 Not supported by all npm commands.
275
276 #### `package-lock-only`
277
278 - Default: false
279 - Type: Boolean
280
281 If set to true, the current operation will only use the `package-lock.json`, ignoring `node_modules`.
282
283 For `update` this means only the `package-lock.json` will be updated, instead of checking `node_modules` and downloading dependencies.
284
285 For `list` this means the output will be based on the tree described by the `package-lock.json`, rather than the contents of `node_modules`.
286
287 #### `package-lock`
288
289 - Default: true
290 - Type: Boolean
291
292 If set to false, then ignore `package-lock.json` files when installing. This will also prevent _writing_ `package-lock.json` if `save` is true.
293
294 #### `omit`
295
296 - Default: 'dev' if the `NODE_ENV` environment variable is set to 'production'; otherwise, empty.
297 - Type: "dev", "optional", or "peer" (can be set multiple times)
298
299 Dependency types to omit from the installation tree on disk.
300
301 Note that these dependencies _are_ still resolved and added to the `package-lock.json` or `npm-shrinkwrap.json` file. They are just not physically installed on disk.
302
303 If a package type appears in both the `--include` and `--omit` lists, then it will be included.
304
305 If the resulting omit list includes `'dev'`, then the `NODE_ENV` environment variable will be set to `'production'` for all lifecycle scripts.
306
307 #### `include`
308
309 - Default:
310 - Type: "prod", "dev", "optional", or "peer" (can be set multiple times)
311
312 Option that allows for defining which types of dependencies to install.
313
314 This is the inverse of `--omit=<type>`.
315
316 Dependency types specified in `--include` will not be omitted, regardless of the order in which omit/include are specified on the command-line.
317
318 #### `foreground-scripts`
319
320 - Default: `false` unless when using `npm pack` or `npm publish` where it defaults to `true`
321 - Type: Boolean
322
323 Run all build scripts (ie, `preinstall`, `install`, and `postinstall`) scripts for installed packages in the foreground process, sharing standard input, output, and error with the main npm process.
324
325 Note that this will generally make installs run slower, and be much noisier, but can be useful for debugging.
326
327 #### `ignore-scripts`
328
329 - Default: false
330 - Type: Boolean
331
332 If true, npm does not run scripts specified in package.json files.
333
334 Note that commands explicitly intended to run a particular script, such as `npm start`, `npm stop`, `npm restart`, `npm test`, and `npm run` will still run their intended script if `ignore-scripts` is set, but they will _not_ run any pre- or post-scripts.
335
336 #### `include-attestations`
337
338 - Default: false
339 - Type: Boolean
340
341 When used with `npm audit signatures --json`, includes the full sigstore attestation bundles in the JSON output for each verified package. The bundles contain DSSE envelopes, verification material, and transparency log entries.
342
343 #### `workspace`
344
345 - Default:
346 - Type: String (can be set multiple times)
347
348 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.
349
350 Valid values for the `workspace` config are either:
351
352 - Workspace names
353 - Path to a workspace directory
354 - Path to a parent workspace directory (will result in selecting all workspaces within that folder)
355
356 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.
357
358 This value is not exported to the environment for child processes.
359
360 #### `workspaces`
361
362 - Default: null
363 - Type: null or Boolean
364
365 Set to true to run the command in the context of **all** configured workspaces.
366
367 Explicitly setting this to false will cause commands like `install` to ignore workspaces altogether. When not set explicitly:
368
369 - 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.
370
371 This value is not exported to the environment for child processes.
372
373 #### `include-workspace-root`
374
375 - Default: false
376 - Type: Boolean
377
378 Include the workspace root when workspaces are enabled for a command.
379
380 When false, specifying individual workspaces via the `workspace` config, or all workspaces via the `workspaces` flag, will cause npm to operate only on the specified workspaces, and not on the root project.
381
382 This value is not exported to the environment for child processes.
383
384 #### `install-links`
385
386 - Default: false
387 - Type: Boolean
388
389 When set file: protocol dependencies will be packed and installed as regular dependencies instead of creating a symlink. This option has no effect on workspaces.
390
391 ### See Also
392
393 - [npm install](/cli/v11/commands/npm-install)
394 - [config](/cli/v11/using-npm/config)