How to audit an npm package before installing it
Practical checks for npm supply-chain risk: install scripts, published files, provenance, maintainers, typosquatting and what the code actually does.
Installing an npm package runs someone else's code on your machine and, eventually, in production. Supply-chain attacks through npm (typosquatted names, hijacked maintainer accounts, malicious install scripts) happen regularly. A few minutes of checks catch the most common problems.
1. Check the name carefully
Typosquatting relies on names that look right: lodahs, react-dom-router, colors-js. Copy the name from the project's official docs or repository instead of typing it. Check the publisher and weekly downloads on the npm page; a near-identical name with tiny downloads is a warning sign.
2. Look at what's published
The code on npm can differ from the code on GitHub. To see exactly what would be installed:
npm view <package>
npm pack <package> --dry-runnpm view shows versions, maintainers, dependencies and the linked repository. npm pack --dry-run lists the files in the published tarball without installing it.
3. Check install scripts
preinstall, install and postinstall scripts run automatically during npm install. Look for them in the package's package.json. Many legitimate packages use them (to download binaries, for example), but they're also the main vector for malicious packages. You can install with --ignore-scripts and run needed scripts deliberately.
4. Check provenance
npm supports provenance attestations, which link a published version to the source repository and build that produced it. Packages with provenance show this on their npm page. npm audit signatures verifies registry signatures and attestations for installed packages.
5. Look at maintainers and recent changes
- Who maintains it? Has ownership changed recently?
- Did a new version appear suddenly after a long gap, with a new maintainer? That pattern has preceded several compromises.
- Read the diff between the version you'd install and the previous one if something feels off.
6. Read the code that runs
Open the main entry file and skim for:
- Network calls in code that shouldn't need the network
- Reading environment variables, SSH keys or credential files
- Obfuscated or minified code in a package that should be readable
eval,new Functionor dynamicrequireof downloaded code
For public packages with a GitHub repository, you can ask RepoGrasp "does this package make network requests or read environment variables, and where?" and get cited locations to inspect. Remember the published tarball can differ from the repository; check the tarball for anything security-critical.
7. Keep checking after install
- Commit your lockfile so installs are reproducible.
- Run
npm auditand review advisories. - Use tools and policies that flag new install scripts or risky updates.
- Pin and review updates to critical dependencies rather than accepting them automatically.
A two-minute version
- Name copied from official docs
npm view: maintainers, repository, recent versions- Install scripts checked
- Entry file skimmed for network, environment and obfuscation
See also evaluating an open-source dependency for the non-security side.