1 ---
2 title: npm-audit
3 section: 1
4 description: Run a security audit
5 github_repo: npm/cli
6 github_branch: release/v7
7 github_path: docs/content/commands/npm-audit.md
8 redirect_from:
9 - /cli-documentation/v7/audit
10 - /cli-documentation/v7/cli-commands/audit
11 - /cli-documentation/v7/cli-commands/npm-audit
12 - /cli-documentation/v7/commands/audit
13 - /cli-documentation/v7/commands/npm-audit
14 - /cli-documentation/v7/npm-audit
15 - /cli/v7/audit
16 - /cli/v7/cli-commands/audit
17 - /cli/v7/cli-commands/npm-audit
18 - /cli/v7/commands/audit
19 - /cli/v7/npm-audit
20 ---
21
22 ### Synopsis
23
24 ```bash
25 npm audit [--json] [--production] [--audit-level=(low|moderate|high|critical)]
26 npm audit fix [--force|--package-lock-only|--dry-run|--production|--only=(dev|prod)]
27
28 common options: [--production] [--only=(dev|prod)]
29 ```
30
31 ### Description
32
33 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.
34
35 The command will exit with a 0 exit code if no vulnerabilities were found.
36
37 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.
38
39 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.
40
41 ### Audit Endpoints
42
43 There are two audit endpoints that npm may use to fetch vulnerability information: the `Bulk Advisory` endpoint and the `Quick Audit` endpoint.
44
45 #### Bulk Advisory Endpoint
46
47 As of version 7, npm uses the much faster `Bulk Advisory` endpoint to optimize the speed of calculating audit results.
48
49 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`.
50
51 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, or one of the shorthands such as `--production`, `--only=dev`, and so on), then packages will be omitted from the submitted payload as appropriate.
52
53 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.
54
55 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`.
56
57 npm then uses these advisory objects to calculate vulnerabilities and meta-vulnerabilities of the dependencies within the tree.
58
59 #### Quick Audit Endpoint
60
61 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.
62
63 The full package tree as found in `package-lock.json` is submitted, along with the following pieces of additional metadata:
64
65 - `npm_version`
66 - `node_version`
67 - `platform`
68 - `arch`
69 - `node_env`
70
71 All packages in the tree are submitted to the Quick Audit endpoint. Omitted dependency types are skipped when generating the report.
72
73 #### Scrubbing
74
75 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.
76
77 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.
78
79 This scrubbing has been removed from npm as of version 7.
80
81 #### Calculating Meta-Vulnerabilities and Remediations
82
83 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.
84
85 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".
86
87 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).
88
89 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.
90
91 ### Exit Code
92
93 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.
94
95 If vulnerabilities were found the exit code will depend on the `audit-level` configuration setting.
96
97 ### Examples
98
99 Scan your project for vulnerabilities and automatically install any compatible updates to vulnerable dependencies:
100
101 ```bash
102 $ npm audit fix
103 ```
104
105 Run `audit fix` without modifying `node_modules`, but still updating the pkglock:
106
107 ```bash
108 $ npm audit fix --package-lock-only
109 ```
110
111 Skip updating `devDependencies`:
112
113 ```bash
114 $ npm audit fix --only=prod
115 ```
116
117 Have `audit fix` install SemVer-major updates to toplevel dependencies, not just SemVer-compatible ones:
118
119 ```bash
120 $ npm audit fix --force
121 ```
122
123 Do a dry run to get an idea of what `audit fix` will do, and _also_ output install information in JSON format:
124
125 ```bash
126 $ npm audit fix --dry-run --json
127 ```
128
129 Scan your project for vulnerabilities and just show the details, without fixing anything:
130
131 ```bash
132 $ npm audit
133 ```
134
135 Get the detailed audit report in JSON format:
136
137 ```bash
138 $ npm audit --json
139 ```
140
141 Fail an audit only if the results include a vulnerability with a level of moderate or higher:
142
143 ```bash
144 $ npm audit --audit-level=moderate
145 ```
146
147 ### Configuration
148
149 #### `audit-level`
150
151 - Default: null
152 - Type: null, "info", "low", "moderate", "high", "critical", or "none"
153
154 The minimum level of vulnerability for `npm audit` to exit with a non-zero exit code.
155
156 #### `dry-run`
157
158 - Default: false
159 - Type: Boolean
160
161 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`.
162
163 Note: This is NOT honored by other network related commands, eg `dist-tags`, `owner`, etc.
164
165 #### `force`
166
167 - Default: false
168 - Type: Boolean
169
170 Removes various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input.
171
172 - Allow clobbering non-npm files in global installs.
173 - Allow the `npm version` command to work on an unclean git repository.
174 - Allow deleting the cache folder with `npm cache clean`.
175 - Allow installing packages that have an `engines` declaration requiring a different version of npm.
176 - Allow installing packages that have an `engines` declaration requiring a different version of `node`, even if `--engine-strict` is enabled.
177 - Allow `npm audit fix` to install modules outside your stated dependency range (including SemVer-major changes).
178 - Allow unpublishing all versions of a published package.
179 - Allow conflicting peerDependencies to be installed in the root project.
180 - Implicitly set `--yes` during `npm init`.
181 - Allow clobbering existing values in `npm pkg`
182
183 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!
184
185 #### `json`
186
187 - Default: false
188 - Type: Boolean
189
190 Whether or not to output JSON data, rather than the normal output.
191
192 - In `npm pkg set` it enables parsing set values with JSON.parse() before saving them to your `package.json`.
193
194 Not supported by all npm commands.
195
196 #### `package-lock-only`
197
198 - Default: false
199 - Type: Boolean
200
201 If set to true, the current operation will only use the `package-lock.json`, ignoring `node_modules`.
202
203 For `update` this means only the `package-lock.json` will be updated, instead of checking `node_modules` and downloading dependencies.
204
205 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`.
206
207 #### `omit`
208
209 - Default: 'dev' if the `NODE_ENV` environment variable is set to 'production', otherwise empty.
210 - Type: "dev", "optional", or "peer" (can be set multiple times)
211
212 Dependency types to omit from the installation tree on disk.
213
214 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.
215
216 If a package type appears in both the `--include` and `--omit` lists, then it will be included.
217
218 If the resulting omit list includes `'dev'`, then the `NODE_ENV` environment variable will be set to `'production'` for all lifecycle scripts.
219
220 #### `workspace`
221
222 - Default:
223 - Type: String (can be set multiple times)
224
225 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.
226
227 Valid values for the `workspace` config are either:
228
229 - Workspace names
230 - Path to a workspace directory
231 - Path to a parent workspace directory (will result to selecting all of the nested workspaces)
232
233 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.
234
235 This value is not exported to the environment for child processes.
236
237 #### `workspaces`
238
239 - Default: false
240 - Type: Boolean
241
242 Enable running a command in the context of **all** the configured workspaces.
243
244 This value is not exported to the environment for child processes.
245
246 ### See Also
247
248 - [npm install](/cli/v7/commands/npm-install)
249 - [config](/cli/v7/using-npm/config)