How to Audit Your package.json for Typosquatting Attacks
Npm typosquatting has caused real-world breaches at companies you've heard of. A single character off in a package name can deliver malicious code to every developer on your team — and every user of your app. Here's how to audit your dependencies before it happens.
What Is Npm Typosquatting?
Typosquatting is the practice of registering package names that are deliberately similar to popular legitimate packages — banking on developers making typos, AI tools hallucinating names, or teams copying package names from memory. When someone accidentally installs the impostor package, they execute the attacker's code.
Unlike traditional malware, typosquatted packages have legitimate-looking names, often minimal README files to appear genuine, and the malicious payload runs silently during installation via postinstall scripts. By the time you notice something is wrong, credentials, tokens, and environment variables may already be exfiltrated.
Real-World Typosquatting Attacks
- crossenv (instead of cross-env) — stole environment variables from CI pipelines
- lodash.clonedeep (instead of lodash's cloneDeep) — injected cryptocurrency miners
- event-source-polyfill variants — multiple malicious versions targeting different typos
- discord.js variants — targeted Discord bot developers to steal tokens
Why AI-Generated Code Makes This Worse
Traditional typosquatting relied on human error — a misplaced letter, a forgotten hyphen. With the rise of AI coding tools, the attack surface has expanded dramatically. AI models:
- Hallucinate package names that don't exist — and malicious actors pre-register these names
- Suggest plausible-sounding alternatives to real packages — often ones that already have typosquatted versions
- Generate large package.json files quickly — too fast for developers to verify each entry manually
- May reference outdated or renamed scoped packages — e.g., suggesting an old
@types/package that has been superseded
Common Typosquatting Patterns to Watch For
Attackers use predictable techniques. Training yourself to recognise these patterns is step one:
How to Audit Your package.json: Step-by-Step
Step 1: Start with a Baseline Check
Before anything else, get a clear picture of what you're working with. Extract all direct dependencies from your package.json and list them for review.
Step 2: Verify Registry Existence
For every package, confirm it exists on the official npm registry. You can check manually using npm info [package-name], but for large dependency lists this becomes tedious quickly.
Step 3: Check for Suspicious Lookalikes
For each dependency, search npm for packages with similar names. A real package should have a clear history, multiple versions, a meaningful description, and a reasonable download count. Be suspicious of:
Step 4: Review Transitive Dependencies
Your direct dependencies bring their own dependencies — the full dependency tree can easily be 10x larger than your package.json suggests. Running npm ls shows the full tree, but it's difficult to audit manually.
Step 5: Automate It
Manual auditing doesn't scale — especially when you're shipping fast with AI tools. The right approach is to integrate automated dependency auditing into your workflow so every package.json is checked before installation and at every CI run.
Prevention: Hardening Your Setup
Use a lockfile and commit it
package-lock.json or yarn.lock pins exact versions and checksums. Never gitignore your lockfile — it's your tamper-proof record of what should be installed.
Enable npm's integrity checking
Use npm ci instead of npm install in CI environments — it strictly validates against your lockfile and fails if there are any discrepancies.
Run a pre-install audit
Before npm install on any AI-generated codebase, scan the package.json first. Catching a typosquatted package before installation means the malicious code never runs on your machine.
Consider a private registry or allowlist
For production environments, proxy npm through a private registry like Verdaccio or Artifactory, where you can enforce an allowlist of approved packages.
The Bottom Line
Typosquatting is not a theoretical risk — it's an active attack vector with documented victims. As AI code generation increases the volume and speed at which developers introduce new dependencies, the attack surface grows proportionally.
The good news: it's one of the most preventable supply chain attacks with the right tooling. Automated auditing before install, lockfile discipline, and CI/CD integration are the three pillars of a robust defence.
Check Your Dependencies for Typosquatting Now
DepAudit detects typosquatted packages, hallucinated names, and known vulnerabilities before you install anything. Free to use, results in seconds.
Scan Your package.json Free