Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

better-npm-audit

jeemok280.4kMIT3.11.0

Reshape into a better npm audit for the community and encourage more people to include security audit into their process.

npm, audit, skip, ignore, exclude, exceptions, node, security, advisory, vulnerabilities, continuous integration, dependencies, check, build, script, nsp, ci

readme

Better NPM Audit

The goal of this project is to provide additional features on top of the existing npm audit options. We hope to encourage more people to do security audits for their projects.

NPM

PRs Welcome npm downloads node current node support npm vulnerability GitHub issues npm bundle size Languages

Looking for Collaborators

Hi there! 👋

I’m currently looking for collaborators to help maintain and develop this project. Due to time constraints, I haven’t been able to give it the attention it deserves, but I believe it has great potential to grow with the help of passionate contributors.

Become a Collaborator If you’re interested in contributing on a regular basis, I’d love to have you on board as a collaborator. Whether you’re interested in fixing bugs, adding new features, or improving documentation, your contributions will be highly valued.

As a collaborator, you’ll have push access to the repository and play a key role in shaping the future of the project. If this sounds like something you’d be interested in, please reach out! You can open an issue titled “Interested in Collaborating” or contact me directly via email.

Let’s work together to make this project even better!

NPM version 6 and 7, and 8

NPM has upgraded to version 7 in late 2020 and has breaking changes on the npm audit. The output of npm audit has significantly changed both in the human-readable and --json output styles. Even more unfortunately, when NPM changed the JSON output in npm v7, they removed many of the other useful identifiers (cves, cwe, github_advisory_id) and the only thing left is the URL. We are trying our best to handle each version and provide consistent functionality to all of them. Related docs on v6 and v7 changes:

Docs Link
NPM v6 & v7 changes https://github.blog/2020-10-13-presenting-v7-0-0-of-the-npm-cli/
NPM v7 blog post https://blog.npmjs.org/post/626173315965468672/npm-v7-series-beta-release-and-semver-major
Official NPM v6 audit docs https://docs.npmjs.com/cli/v6/commands/npm-audit
Official NPM v7 audit docs https://docs.npmjs.com/cli/v7/commands/npm-audit
Dealing with new npm audit https://uko.codes/dealing-with-npm-v7-audit-changes

You may find the sample JSON outputs for each NPM versions in our codebase: v6, v7 & v8.


Installation

$ npm install --save better-npm-audit

or

$ npm install -g better-npm-audit

Usage

Run global

better-npm-audit audit

Run with exceptions

Demo of table displaying the security report

Unhandled or newly reported vulnerabilities will be highlighted:

Demo of table displaying the security report

Unused exceptions will be notified:

Demo of displaying the unused exception

Add into package scripts

{
  "scripts": {
    "prepush": "npm run test && npm run audit",
    "audit": "better-npm-audit audit"
  }
}

Now you can run locally or in your CI pipeline:

npm run audit

Options

Flag Short Description
--exclude -x Exceptions or the vulnerabilities ID(s) to exclude; the ID can be the numeric ID, CVE, CWE or GHSA ID
--module-ignore -m Names of modules to exclude
--level -l The minimum audit level to validate; Same as the original --audit-level flag
--production -p Skip the devDependencies
--registry -r The npm registry url to use
--include-columns -i Columns to include in report

Environment Variables

Variable Description
NO_COLOR Support the no-color standard to allow users use the tool without colored output.
NPM_CONFIG_AUDIT_LEVEL Used in setting the audit level.
Note: this will be disregard if the audit level flag is passed onto the command.

Using .nsprc file to manage exceptions

You may add a file .nsprc to your project root directory to manage the exceptions. For example:

{
  "1337": {
    "active": true,
    "notes": "Ignored since we don't use xxx method",
    "expiry": 1615462134681
  },
  "4501": {
    "active": false,
    "notes": "Ignored since we don't use xxx method"
  },
  "CWE-471": "CWE ID is acceptable",
  "GHSA-ww39-953v-wcq6": "GHSA ID is acceptable",
  "https://npmjs.com/advisories/1213": "Full or partial URL is acceptable too"
}

Fields

Attribute Type Description Default Examples
active Boolean If the tool should use it for exception true true
expiry String | Number Human-readable date, or milliseconds since the UNIX Epoch | - '2020-01-31'
- '2020/01/31'
- '01/31/2021, 11:03:58'
- '1 March 2016 15:00'
- '1 March 2016 3:00 pm'
- '2012-01-26T13:51:50.417-07:00'
- 'Sun, 11 Jul 2021 03:03:13 GMT'
- 'Thu Jan 26 2017 11:00:00 GMT+1100 (Australian Eastern Daylight Time)'
- 327611110417
notes String Notes related to the vulnerability.

When using a .nsprc file, a report will be displayed when it starts running:

Demo of table displaying a list of exceptions

Note: the expiry date will be styled in yellow and red color if it is detected more than one or five years ago.


Changelog

You can find the changelog here.


Contributors

Ian Wright, Edwin Taylor, Maarten Hus, Alex Burkowsky, David M. Lee, Kyle Clark, Guillermo Pincay, Grzegorz Pawłowski, CSLTech, Paul Clarkin, mgdodge, Ricky Sullivan, Sam Gregory, Tristan WAGNER, Zak, Eric Cornelissen, Gaurav Chinavle



If you like this project,

Buy Me A Coffee

changelog

3.11.0 (September 9, 2024)

  • #102 Respect the NO_COLOR environment variable

3.10.0 (September 3, 2024)

  • #100 Add support for including specific columns in audit report

3.9.0 (September 2, 2024)

  • 5df4120 Bump micromatch from 4.0.4 to 4.0.8
  • 8820f03 Fix getting npm version through CLI

3.8.1 - 3.8.3 (August 17, 2024)

  • 607f16e fix(build): ensure lib/index.js is executable after build

3.8.0 (August 17, 2024)

  • 27a7cb3 Use "--omit=dev" internally on newer npm version
  • 76b4c57 b3e04d3 NPM Audit for fixing vulnerabilities

3.7.3 (March 22, 2022)

  • 99c0697 Added handling for empty strings in displaying unused exception message

3.7.2 (March 21, 2022)

3.7.1 (March 19, 2022)

  • 43380eb Fixed unused exceptions handler

3.7.0 (March 10, 2022)

  • 1871068 Handles non numeric exception IDs

3.6.0 (February 23, 2022)

  • #71 Added new option: ignore by module name

3.5.1 (December 1, 2021)

  • 0316010 Fixed npm run audit command
  • 697421d Fixed hanging process on Windows
  • f5ebe1f Fixed invalid main path in package.json

3.4.0 (October 10, 2021)

  • fe66222 Log exception, when failed to parse .nsprc file

3.3.0 (August 15, 2021)

  • 5f36c41 Shorten node path for NPM v7

3.2.1 (August 7, 2021)

  • e5d19a5 Include dependency path into the security report

3.1.2 (July 18, 2021)

3.1.1 (July 14, 2021)

3.1.0 (July 11, 2021)

  • #a5bba42 Updated declaration file extension so it will not be included in final build
  • #b1b05ff Added multiple date format support for expiry field
  • #15ae9ad Added dayjs package
  • #32b8535 Style the exception expiry date if it is more than one or five years ago

3.0.1 (July 11, 2021)

3.0.0 (July 11, 2021)

  • #49 Refactored to TypeScript 🎉
  • #49 Upgraded package commander from version 2.19.0 to 8.0.0

2.1.0 (June 24, 2021)

  • #43 Add support for npm registry url option (@Tristan WAGNER)
  • #42 Added CodeQL vulnerabilities check across codebase in CI
  • #e77632c Removed github username as region currently not supported

2.0.5 (June 22, 2021)

  • #52be395 Removed unused package cli-table from the dependencies
  • #40 Added nodejs v16.x coverage in CI

2.0.4 (June 22, 2021)

Notable changes

  • #0b7357c Simplified the workflow and improved overall performance by running lesser in the process
  • #0b7357c Added table module to display table format reports
  • #0b7357c Added table display for security report
  • #0b7357c Added table display of exceptions from .nsprc file
  • #39 Cleaned up test cases structure to be more straight forward and easier to maintain

Breaking changes

  • #e08a436 Renamed --ignore -i flag to --exclude -x for better clarity
  • #0b7357c Removed --display-full flag that was used to ignore the maximum display limit
  • #0b7357c Removed --display-notes flag that was used for displaying exception notes
  • #0b7357c Renamed ignore field to active in .nsprc file for better clarity
  • #0b7357c Renamed reason field to notes in .nsprc file for better clarity

Others

  • #4ba2612 Updated wording of unused exception warning
  • #0b7357c Removed logging of flags used in the command
  • #0b7357c Added NPM audit into the CI pipeline
  • #39 Updated README

Closed issues

  • #20 Provide more output when parsing exceptions file
  • #27 Hide excepted vulnerabilities from output
  • #28 Missing [ in truncation message

1.12.1 (June 21, 2021)

  • #7249096 Added FUNDING & updated README

1.12.0 (June 18, 2021)

1.11.2 (June 11, 2021)

  • #37 Fixed security CVE-2020-28469: Bump glob-parent from 5.1.1 to 5.1.2

1.11.1 (June 11, 2021)

1.11.0 (June 11, 2021)

  • #36 Added environment variable support NPM_CONFIG_AUDIT_LEVEL

1.10.1 (June 7, 2021)

  • #6661c78 Updated --full flag logging from [full log mode enabled] to [report display limit disabled]
  • #32 Added new flag --display-notes to display reasons for the exceptions

1.9.3 (June 6, 2021)

  • #31 Added CHANGELOG
  • #33 Updated README