Key Takeaways
- A transitive dependency is a package you get because something you depend on needs it.
- One npm package can drag in hundreds of transitive dependencies, each able to run code with your access.
- Other maintainers set the ranges deep in your tree, so fixes and attacks arrive on their schedule.
- Protection has to work at the registry level to reach packages your team has never reviewed.
Try this on any project you’ve been working on for more than a few months. Open package.json and count the dependencies. Then run npm ls --all and watch how much scrolls past.
The list in package.json is usually somewhere in the dozens. The tree is a lot longer, even with npm marking repeat entries as deduped. Everything in that tree that you didn’t list yourself is a transitive dependency, and it’s the part of your stack that gets the least attention while doing an awful lot of the work.
What a transitive dependency actually is
A direct dependency is one you asked for. You ran npm install express, it landed in package.json, and somebody on your team made a decision about it.
A transitive dependency is one you didn’t ask for. Express needs its own packages, those packages need theirs, and npm resolves the whole chain for you. None of it shows up in your project’s package.json, but all of it is recorded in your lockfile, and whatever actually gets installed lands in node_modules. Any of it that runs, whether during install or when your app loads it, gets the same access as whatever is running it. On your laptop that means your user account, and in CI it means the runner and every secret it can see.
It helps to give this a name, so call it inherited risk. Inherited risk is the exposure your project takes on from packages that came along with the ones you chose. You didn’t evaluate them, you didn’t pick their versions, and in most cases you couldn’t name them. You still ship them.
Why the tree gets so big
Part of it is just how the JavaScript ecosystem grew up. npm made publishing tiny single-purpose packages easy, and the culture ran with it. A utility that does one thing well gets pulled into a hundred other packages, and each of those brings its own small helpers along. As TuxCare’s SecureChain for npm page puts it, one top-level npm package can pull in hundreds of transitive dependencies.
The other part is how npm lays out node_modules. Wherever it can, npm hoists shared packages up the tree so most of them sit side by side near the top. That’s great for disk space and terrible for intuition. Scroll through node_modules and nothing tells you that a package is four layers deep and only there because something you’ve never heard of asked for it.
If you want to see the layers, npm explain <package> will show you every path that leads to a given package. Run it on something random from your lockfile and don’t be surprised if more than one chain leads to it.
You don’t own the version ranges
This is the part that trips people up. When you add a dependency, you choose its version range. When that dependency adds its own, the maintainer chooses the range, and you inherit it.
Most of those ranges use a caret, like ^4.17.0, which semver treats as any 4.x release from 4.17.0 up. Your lockfile pins the exact versions that resolved last time, which is why committing it and using npm ci in CI matters so much. But the moment someone deletes and regenerates the lockfile, runs an update or installs into a project without one, ranges across the tree can resolve to the newest version they allow, including one that was published an hour ago.
That’s convenient when a maintainer ships a fix. It’s a problem when they ship something else.
Attackers figured this out a long time ago. Back in 2018, a new maintainer of the popular event-stream package added a dependency called flatmap-stream, which turned out to carry code aimed at stealing from a specific Bitcoin wallet app. Nobody using event-stream had chosen flatmap-stream. It just arrived.
The same move showed up again in June 2026. When attackers got publishing access to the Mastra npm scope, they pushed more than 140 compromised package versions in under two hours, according to reporting from The Hacker News and Socket. The Mastra code itself was untouched. What changed was the dependency list, which now included easy-day-js, a lookalike of the dayjs date library. Whoever controlled easy-day-js had published a clean, working version 1.11.21 the day before, then shipped 1.11.22 with a malicious postinstall hook early on June 17, shortly before the Mastra releases went out. The new Mastra versions asked for ^1.11.21, so every fresh install resolved straight to the poisoned release. If you’d reviewed one of those releases, the only real change you’d have found in package.json was a single new dependency. Everything dangerous lived one level down.
Why fixing a deep vulnerability is so slow
Known vulnerabilities pile up deep in the tree too. When Endor Labs analyzed widely used Java packages for its 2022 State of Dependency Management report, it found that roughly 95% of the vulnerable dependencies were transitive. Those were Maven packages, so take the number as a sense of scale for npm rather than an npm figure.
Say a CVE lands in a package three levels below anything you chose. The fix gets released upstream, and in a perfect world your tree just picks it up. In practice, that only works if every package between you and the fix allows the new version. If any parent in the chain pinned an exact version, or capped its range below the fixed release, normal resolution stops right there. Now you’re waiting for that maintainer to cut a release, and possibly the maintainer above them too.
This is why npm audit can feel so frustrating. It’s good at telling you something is wrong deep in the tree. It’s much worse at fixing the problem, because the fix often isn’t yours to make. npm audit fix won’t apply a semver-major change without --force, and --force is a fairly blunt way to find out which parts of your app break.
Then there are overrides, which npm added in version 8.3. An override lets you force a specific version of a transitive package, or swap in a fork, regardless of what its parent asked for. It can clear the vulnerability. Just be clear about what you’ve done. You’re now running a combination of packages the parent’s maintainer probably never tested, and the override tends to sit in your package.json long after anyone remembers why it’s there.
The maintainer who isn’t coming back
All of that assumes someone upstream is still shipping releases. Deep in a large tree, that’s not always the case.
Plenty of the packages near the bottom of a tree are small and essentially finished, and some of their maintainers moved on years ago. When a CVE lands in one of those, there’s nobody to cut the fix, so there’s nothing to propagate upward. The scanner keeps flagging it, and there’s no upstream fix for an override to point at. What’s left is forking and patching it yourself, finding someone who’ll backport a fix, or replacing whatever top-level package depends on it. None of those are quick, and the last one can turn into a rewrite nobody has time for.
The backport route is what TuxCare’s Endless Lifecycle Support exists for. For the packages it covers, ELS backports security fixes to the exact versions you’re already running once upstream maintenance ends, so you can stay where you are instead of being forced into an upgrade.
Protecting the whole tree
Most of the fixes above happen inside your project, one package at a time, and usually after the problem has already landed in your tree. Overrides, audit fixes and manual pins are all ways of cleaning up something that arrived from upstream.
The alternative is to change where the whole tree comes from. That’s the idea behind SecureChain. It replaces the public npm registry with a curated repository that your installs resolve from, transitive packages included. Packages are rebuilt from verified source in hardened, isolated environments and screened for known malware and suspicious code patterns, and anything that fails inspection is blocked before it reaches your environment. If something your tree needs isn’t in the catalog yet, TuxCare adds it. Active packages are patched under SLA as new CVEs come out, and when a package SecureChain maintains reaches end of life, ELS continues coverage with no action needed on your side.
Once you point npm or your repository manager at it, day-to-day work doesn’t change. npm, Yarn and pnpm keep the same commands, and SecureChain works alongside Artifactory, Nexus or GitHub Packages if that’s how your team already proxies packages. What changes is that the package four levels down, the one nobody has ever heard of, goes through the same rebuild and screening as the framework you chose on purpose.
Where to start
Before you change anything, get a clear picture of what you’re running. Run npm ls --all and save the output so you can compare it later. Pick a few packages you don’t recognize and run npm explain on them. Make sure your lockfile is committed and that CI uses npm ci, so builds install from the lockfile instead of re-resolving ranges, and versions only move when someone commits an updated lockfile.
Then look at your last few audit reports and sort the findings into packages you chose and packages you inherited. The second pile is a good map of where your inherited risk actually sits, and it’s the one most worth a closer look.
FAQ
What is a transitive dependency?
A transitive dependency is a package your project installs because one of your dependencies needs it, not because you added it yourself. In npm, transitive dependencies are recorded in your lockfile and, once installed, show up in node_modules, but they aren’t declared in your project’s package.json.
What’s the difference between direct and transitive dependencies?
Direct dependencies are the packages your project declares in its own package.json. Transitive dependencies are everything those packages pull in, at every level of the tree. You control the version ranges of direct dependencies, while the ranges of transitive ones are set by their parent packages.
How do I see my transitive dependencies in npm?
Run npm ls --all to print your full dependency tree. To find out why a specific package is installed, run npm explain <package>, which shows every dependency path that leads to it.
How do I fix a vulnerability in a transitive dependency?
First, check whether updating the direct dependency above it pulls in the fixed version. If the parent’s version range blocks the fix, you can use npm overrides, available since npm 8.3, to force a patched version, though you’ll be running a combination the parent’s maintainer probably hasn’t tested. If no fixed version exists because the package is unmaintained, your options are a backported fix, your own fork or patch, or replacing the package that depends on it.
Are transitive dependencies a security risk?
Yes. Any of their code that runs gets the same access as your build or your app, some of them run install scripts, and a single top-level package can bring in hundreds of them. Supply chain attacks, including event-stream in 2018 and the Mastra compromise in 2026, delivered their payload through a newly added transitive dependency.
What is inherited risk?
Inherited risk is the security exposure a project takes on from packages that came along with the dependencies it chose. It covers vulnerabilities, malicious code and abandoned packages anywhere in the transitive dependency tree.
Sources
- SecureChain for npm, TuxCare
- SecureChain for Open Source Software, TuxCare
- Endless Lifecycle Support, TuxCare
- State of Dependency Management 2022, Endor Labs
- 145 Mastra npm Packages Compromised via Hijacked Contributor Account, The Hacker News
- Mastra npm Packages Compromised, Socket
- Node.js event-stream module compromise, The Hacker News
- How to override your dependency’s dependencies, Stefan Judis
- npm ls, npm Docs
- npm explain, npm Docs
- npm dedupe, npm Docs
- npm ci, npm Docs
- npm audit, npm Docs
- package.json overrides, npm Docs
- package-lock.json, npm Docs
- Caret ranges, node-semver
