Skip to main content

Common Misconfigurations

  1. Ignoring npm audit warnings
  2. Not updating dependencies regularly
  3. Using packages with known vulnerabilities
  4. Not checking transitive dependencies
  5. Missing security audit in CI/CD pipeline

Vulnerable Example

Secure Solution

Key Commands for Updating

Here are the essential npm commands to find and update vulnerable or outdated packages.

1. Audit for Vulnerabilities

This command scans your project against the npm registry for known security vulnerabilities. It’s the command used in the scripts section.

2. Automatically Fix Vulnerabilities

This command attempts to automatically update your package-lock.json to fix the vulnerabilities found by npm audit. It will only perform “safe” updates that respect your package.json version ranges.
  • Forcing Fixes: If vulnerabilities remain (often due to major version changes), you can try npm audit fix --force. Warning: This can install breaking changes, so use it with caution and test your application thoroughly afterward.

3. Check for Outdated Packages

This command lists all dependencies that have newer versions available than what is currently installed. This checks for all updates, not just security patches.

4. Update Packages to Latest SemVer

This command updates your packages to the latest version that is allowed by the semantic versioning (semver) rules in your package.json (e.g., ^4.17.20 might update to 4.17.21 but not to 5.0.0).

5. Update a Specific Package

To update a single package to its absolute latest version (even a new major version) and save that new version to your package.json:
For a more powerful way to update all packages to their latest major versions (which npm update won’t do), you can use the npm-check-updates tool.

Best Practices

  • Run npm audit regularly.
  • Keep package-lock.json in version control.
  • Use automated dependency updates (Dependabot/Renovate).
  • Implement security checks in CI/CD.
  • Review and update dependencies monthly.