You’ve typed npm install a thousand times. You’ve probably also typo’d a package name at least once, and gotten lucky. Not everyone does. Attackers count on that single lapse – the trust in muscle memory, the quick hit of enter – and the same package ecosystem that makes JavaScript development so fast is what makes typosquatting so effective.
This post breaks down what typosquatting is, how to spot it, and what you can actually do to protect your projects.
What Is Typosquatting?
Typosquatting is the practice of publishing a malicious package under a name that closely resembles a popular, legitimate one, with the hope that developers will mistype the name, misread it, copy it from an untrusted source, or fail to double-check before installing.
A few common patterns include:
- Character swaps or omissions/mispelings: lodash becomes lodahs or loadsh
- Hyphen tricks: babel-core vs babelcore
- Pluralization: colors vs color
- Scope and namespace tricks: @types/react (a real scoped package) vs. a fake, unscoped types-react
- Homoglyphs: replacing a lowercase l with a capital I, or using other look-alike characters
Once installed, these packages can do anything a real npm package can do: read environment variables, exfiltrate .env files or SSH keys, install backdoors, or quietly inject malicious code into your build output. Because npm lifecycle scripts can run during installation, the damage can happen before you’ve even looked at the code.
Typosquatting isn’t limited to packages. It shows up in URLs too. Take, for instance, guthub.com. If you’ve ever meant to type “github.com” and landed on “guthub.com” instead, you got lucky: that domain redirects to the real site. But that redirect is a courtesy someone set up, not a guarantee. Whoever registers a common misspelling controls where it points, and if a malicious party owns it, a single slipped keystroke can drop you on a credential-harvesting clone. In the package ecosystem the danger is even more direct, because the same kind of mistake can pull executable code straight into your project.
Why JavaScript Specifically?
This isn’t unique to JavaScript, but the npm ecosystem is a particularly attractive target. The registry is enormous, many packages are maintained by small teams or individuals, and modern dependency trees can run hundreds or thousands of packages deep because of nested transitive dependencies. Combine that with low publishing barriers and the casual, glance-and-go nature of dependency installation, and typosquatting has plenty of room to succeed.
The same risk follows you across package managers. Only the command changes:
yarn add package_name pnpm add package_name bun add package_name
Whether you use yarn, pnpm, or bun, the syntax differs from npm: all three use add instead of install. But they pull from the same source – the npm registry – so the command changes while the trust problem doesn’t. A mistyped or maliciously injected package can reach your project no matter which manager you use, even if you never type npm install yourself.
npm also doesn’t revoke a maintainer’s publish access just because an account goes quiet, and the June 2026 Mastra attack showed what that enables. Attackers took over the account of a former Mastra contributor whose access to the @mastra scope had never been revoked, then injected easy-day-js — itself a typosquat of the popular dayjs library — into more than 140 Mastra packages. A trusted scope became a delivery path for a malicious lookalike dependency.
How to Identify a Typosquatted Package
Red flags aren’t always obvious, but a few checks can help you catch a suspicious package before it enters your project:
Look at download counts and publish history. A package claiming to be a “popular” library but with a few hundred downloads and a publish date from last week is a red flag. Compare it against the real package’s stats on npmjs.com.
Check the maintainer. Does the publishing account match the known maintainers of the real project? A brand-new account, an unfamiliar maintainer, or a package with no meaningful publishing history is worth a second look.
Read the package.json carefully. Look at the repository URL, license, and description. Typosquats often copy these fields directly from the legitimate package, but sometimes the repo link points somewhere unexpected, or is missing entirely.
Inspect install scripts before they run. Inspect install scripts before they run. preinstall, install, and postinstall hooks in package.json deserve extra scrutiny because they can execute code during installation. If a simple utility library ships an obfuscated postinstall script that reaches out to a remote URL, treat it as a serious warning sign — that’s exactly how the easy-day-js dependency worked in the Mastra attack, fetching and running a second-stage payload.
Use the official registry search, not a guess. Search npmjs.com, copy the install command from the project’s official documentation, or rely on your IDE’s autocomplete rather than typing unfamiliar package names from memory. Muscle memory is useful right up until it installs the wrong package.
Watch for last-minute dependency changes. If a package-lock.json or yarn.lock , or pnpm-lock.yaml diff shows a dependency name change you didn’t expect, stop and investigate before merging. The same applies to any newly added package that nobody on the team intentionally introduced.
Practical Defenses for Your Project
Manual review helps, but it doesn’t scale on its own. The stronger approach is to build structural protections into how your team installs, reviews, and promotes dependencies:
Lock your dependencies. Commit your package-lock.json or yarn.lock file and use npm ci in CI/CD pipelines instead of npm install. This ensures builds use exactly the versions you’ve vetted, not whatever resolves at install time.
Use automated scanning tools. npm audit, Snyk, Socket, and GitHub Dependabot — collectively known as software composition analysis (SCA) tools — can flag known vulnerabilities, malicious packages, and unexpected install scripts. Socket in particular is built specifically to detect supply-chain risks like typosquatting and rogue postinstall hooks. None of these catches everything, but together they give your team a much better chance of spotting risk before it reaches production.
Pin exact versions for critical dependencies. Avoid overly loose semver ranges (^ or ~) for packages that touch sensitive operations, so an automatic update can’t silently introduce a compromised version. Pinning also keeps package behavior consistent across teams, environments, and CI/CD pipelines.
Disable scripts by default when it makes sense.
Disable scripts by default where it makes sense. Running installs with –ignore-scripts in CI, then explicitly allow-listing the packages that legitimately need build steps, reduces the blast radius of a malicious postinstall hook. pnpm is worth considering here too: since v10 it blocks dependency lifecycle scripts by default and requires you to approve them through an allow-list.
Running npm install –ignore-scripts in CI, then explicitly allow-listing packages that legitimately need install scripts, reduces the blast radius of a malicious postinstall hook. This is a great use case for pnpm. Pnpm ignores install scripts by default since v10. You can use the –ignore-scripts flag at the command line level, the .npmrc level, and the global level.
Adopt a private registry or proxy. Tools like Verdaccio, Sonatype Nexus, JFrog Artifactory, or npm’s own private registry features let teams mirror, approve, and control packages before they’re available to developers. That gives you a checkpoint where suspicious lookalikes can be blocked before they ever reach a developer’s machine.
Review new dependencies as part of code review. Treat adding a new package the way you’d treat adding a new contributor with access to your codebase, because functionally that’s close to what you’re doing. Create a security policy that defines how new packages get approved, what reviewers should check, and which sources your team is allowed to install from. Tools like SecureChain can support this by giving teams a curated source of vetted open-source packages.
Educate your team. A five-minute conversation about typosquatting patterns can prevent a costly incident. Make it part of onboarding for any role that touches package.json. Create a team policy and make sure to add security checks as a top priority, and enforce it.
A Quick Mental Checklist
Before adding ANY new dependency, ask:
- Is this the name I actually intended to type?
- Does the download count and publish history look legitimate?
- Does the source repository match a project I recognize or can verify?
- Are there install scripts, and do I understand what they do, why they exist, and whether they’re necessary?
- Has my scanning tool flagged anything?
Final Thoughts
Typosquatting is effective because it exploits trust and routine, two things developers rely on constantly. The good news is that reducing the risk doesn’t require overhauling your entire workflow.
Moving from muscle memory to verified package names, locked dependency trees, automated scanning, approved registries, and a moment of healthy skepticism before each new install will close off the vast majority of this attack surface.

