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