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 The npm CLI supports registry signatures and signing keys provided by any registry if the following conventions are followed:
71
72 1. Signatures are provided in the package's `packument` in each published version within the `dist` object:
73
74 ```json
75 "dist":{
76 "..omitted..": "..omitted..",
77 "signatures": [{
78 "keyid": "SHA256:{{SHA256_PUBLIC_KEY}}",
79 "sig": "a312b9c3cb4a1b693e8ebac5ee1ca9cc01f2661c14391917dcb111517f72370809..."
80 }]
81 }
82 ```
83
84 See this [example](https://registry.npmjs.org/light-cycle/1.4.3) of a signed package from the public npm registry.
85
86 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.
87
88 2. Public signing keys are provided at `registry-host.tld/-/npm/v1/keys` in the following format:
89
90 ```
91 {
92 "keys": [{
93 "expires": null,
94 "keyid": "SHA256:{{SHA256_PUBLIC_KEY}}",
95 "keytype": "ecdsa-sha2-nistp256",
96 "scheme": "ecdsa-sha2-nistp256",
97 "key": "{{B64_PUBLIC_KEY}}"
98 }]
99 }
100 ```
101
102 Keys response:
103
104 - `expires`: null or a simplified extended [ISO 8601 format](https://en.wikipedia.org/wiki/ISO_8601): `YYYY-MM-DDTHH:mm:ss.sssZ`
105 - `keydid`: sha256 fingerprint of the public key
106 - `keytype`: only `ecdsa-sha2-nistp256` is currently supported by the npm CLI
107 - `scheme`: only `ecdsa-sha2-nistp256` is currently supported by the npm CLI
108 - `key`: base64 encoded public key
109
110 See this [example key's response from the public npm registry](https://registry.npmjs.org/-/npm/v1/keys).
111
112 ### Audit Endpoints
113
114 There are two audit endpoints that npm may use to fetch vulnerability information: the `Bulk Advisory` endpoint and the `Quick Audit` endpoint.
115
116 #### Bulk Advisory Endpoint
117
118 As of version 7, npm uses the much faster `Bulk Advisory` endpoint to optimize the speed of calculating audit results.
119
120 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`.
121
122 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.
123
124 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.
125
126 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`.
127
128 npm then uses these advisory objects to calculate vulnerabilities and meta-vulnerabilities of the dependencies within the tree.
129
130 #### Quick Audit Endpoint
131
132 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.
133
134 The full package tree as found in `package-lock.json` is submitted, along with the following pieces of additional metadata:
135
136 - `npm_version`
137 - `node_version`
138 - `platform`
139 - `arch`
140 - `node_env`
141
142 All packages in the tree are submitted to the Quick Audit endpoint. Omitted dependency types are skipped when generating the report.
143
144 #### Scrubbing
145
146 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.
147
148 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.
149
150 This scrubbing has been removed from npm as of version 7.
151
152 #### Calculating Meta-Vulnerabilities and Remediations
153
154 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.
155
156 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".
157
158 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).
159
160 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.
161
162 ### Exit Code
163
164 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.
165
166 If vulnerabilities were found the exit code will depend on the [`audit-level` config](/cli/v11/using-npm/config#audit-level).
167
168 ### Examples
169
170 Scan your project for vulnerabilities and automatically install any compatible updates to vulnerable dependencies:
171
172 ```bash
173 $ npm audit fix
174 ```
175
176 Run `audit fix` without modifying `node_modules`, but still updating the pkglock:
177
178 ```bash
179 $ npm audit fix --package-lock-only
180 ```
181
182 Skip updating `devDependencies`:
183
184 ```bash
185 $ npm audit fix --only=prod
186 ```
187
188 Have `audit fix` install SemVer-major updates to toplevel dependencies, not just SemVer-compatible ones:
189
190 ```bash
191 $ npm audit fix --force
192 ```
193
194 Do a dry run to get an idea of what `audit fix` will do, and _also_ output install information in JSON format:
195
196 ```bash
197 $ npm audit fix --dry-run --json
198 ```
199
200 Scan your project for vulnerabilities and just show the details, without fixing anything:
201
202 ```bash
203 $ npm audit
204 ```
205
206 Get the detailed audit report in JSON format:
207
208 ```bash
209 $ npm audit --json
210 ```
211
212 Fail an audit only if the results include a vulnerability with a level of moderate or higher:
213
214 ```bash
215 $ npm audit --audit-level=moderate
216 ```
217
218 ### Configuration
219
220 #### `audit-level`
221
222 - Default: null
223 - Type: null, "info", "low", "moderate", "high", "critical", or "none"
224
225 The minimum level of vulnerability for `npm audit` to exit with a non-zero exit code.
226
227 #### `dry-run`
228
229 - Default: false
230 - Type: Boolean
231
232 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`.
233
234 Note: This is NOT honored by other network related commands, eg `dist-tags`, `owner`, etc.
235
236 #### `force`
237
238 - Default: false
239 - Type: Boolean
240
241 Removes various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input.
242
243 - Allow clobbering non-npm files in global installs.
244 - Allow the `npm version` command to work on an unclean git repository.
245 - Allow deleting the cache folder with `npm cache clean`.
246 - Allow installing packages that have an `engines` declaration requiring a different version of npm.
247 - Allow installing packages that have an `engines` declaration requiring a different version of `node`, even if `--engine-strict` is enabled.
248 - Allow `npm audit fix` to install modules outside your stated dependency range (including SemVer-major changes).
249 - Allow unpublishing all versions of a published package.
250 - Allow conflicting peerDependencies to be installed in the root project.
251 - Implicitly set `--yes` during `npm init`.
252 - Allow clobbering existing values in `npm pkg`
253 - Allow unpublishing of entire packages (not just a single version).
254
255 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!
256
257 #### `json`
258
259 - Default: false
260 - Type: Boolean
261
262 Whether or not to output JSON data, rather than the normal output.
263
264 - In `npm pkg set` it enables parsing set values with JSON.parse() before saving them to your `package.json`.
265
266 Not supported by all npm commands.
267
268 #### `package-lock-only`
269
270 - Default: false
271 - Type: Boolean
272
273 If set to true, the current operation will only use the `package-lock.json`, ignoring `node_modules`.
274
275 For `update` this means only the `package-lock.json` will be updated, instead of checking `node_modules` and downloading dependencies.
276
277 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`.
278
279 #### `package-lock`
280
281 - Default: true
282 - Type: Boolean
283
284 If set to false, then ignore `package-lock.json` files when installing. This will also prevent _writing_ `package-lock.json` if `save` is true.
285
286 #### `omit`
287
288 - Default: 'dev' if the `NODE_ENV` environment variable is set to 'production', otherwise empty.
289 - Type: "dev", "optional", or "peer" (can be set multiple times)
290
291 Dependency types to omit from the installation tree on disk.
292
293 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.
294
295 If a package type appears in both the `--include` and `--omit` lists, then it will be included.
296
297 If the resulting omit list includes `'dev'`, then the `NODE_ENV` environment variable will be set to `'production'` for all lifecycle scripts.
298
299 #### `include`
300
301 - Default:
302 - Type: "prod", "dev", "optional", or "peer" (can be set multiple times)
303
304 Option that allows for defining which types of dependencies to install.
305
306 This is the inverse of `--omit=<type>`.
307
308 Dependency types specified in `--include` will not be omitted, regardless of the order in which omit/include are specified on the command-line.
309
310 #### `foreground-scripts`
311
312 - Default: `false` unless when using `npm pack` or `npm publish` where it defaults to `true`
313 - Type: Boolean
314
315 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.
316
317 Note that this will generally make installs run slower, and be much noisier, but can be useful for debugging.
318
319 #### `ignore-scripts`
320
321 - Default: false
322 - Type: Boolean
323
324 If true, npm does not run scripts specified in package.json files.
325
326 Note that commands explicitly intended to run a particular script, such as `npm start`, `npm stop`, `npm restart`, `npm test`, and `npm run-script` will still run their intended script if `ignore-scripts` is set, but they will _not_ run any pre- or post-scripts.
327
328 #### `workspace`
329
330 - Default:
331 - Type: String (can be set multiple times)
332
333 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.
334
335 Valid values for the `workspace` config are either:
336
337 - Workspace names
338 - Path to a workspace directory
339 - Path to a parent workspace directory (will result in selecting all workspaces within that folder)
340
341 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.
342
343 This value is not exported to the environment for child processes.
344
345 #### `workspaces`
346
347 - Default: null
348 - Type: null or Boolean
349
350 Set to true to run the command in the context of **all** configured workspaces.
351
352 Explicitly setting this to false will cause commands like `install` to ignore workspaces altogether. When not set explicitly:
353
354 - 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.
355
356 This value is not exported to the environment for child processes.
357
358 #### `include-workspace-root`
359
360 - Default: false
361 - Type: Boolean
362
363 Include the workspace root when workspaces are enabled for a command.
364
365 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.
366
367 This value is not exported to the environment for child processes.
368
369 #### `install-links`
370
371 - Default: false
372 - Type: Boolean
373
374 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.
375
376 ### See Also
377
378 - [npm install](/cli/v11/commands/npm-install)
379 - [config](/cli/v11/using-npm/config)