Definition
A transitive dependency (or indirect dependency) is a package your project installs because one of your dependencies needs it, not because you listed it yourself. Direct dependencies are the ones named in your manifest; everything they pull in below that is transitive.
Direct and transitive dependencies
If your package.json lists only express, then express is a direct dependency. It needs body-parser, which needs debug, which needs ms. Those three are transitive: they are installed and they run in your application, but their names appear only in the lockfile.
Most of a dependency tree is transitive. A new web application lists a handful of direct dependencies and installs several hundred packages.
How to find where a transitive dependency comes from
npm explain ms # which packages require ms, and which versions npm ls ms # the same, as a tree pnpm why ms yarn why ms pipdeptree -r -p urllib3 cargo tree -i syn go mod why -m golang.org/x/text
How to change the version of a transitive dependency
You cannot fix a transitive dependency by editing your own version ranges, because another package chooses it. Each package manager has an override:
| Tool | Setting |
|---|---|
| npm | overrides in package.json |
| Yarn | resolutions in package.json |
| pnpm | overrides in pnpm-workspace.yaml (or pnpm.overrides in package.json) |
| pip | a constraints file: pip install -c constraints.txt |
| Cargo | cargo update -p name --precise 1.2.3, or [patch] |
| Go | go get module@version, or a replace directive |
{
"overrides": {
"ms": "2.1.3"
}
}
Why transitive dependencies are a security problem
- Nobody on your team chose them. They were not reviewed, and most developers cannot name them.
- They change without a change in your manifest. Without a committed lockfile, a new release of a package four levels down is installed the next time someone runs an install.
- Attackers aim at them. In 2018 the popular
event-streampackage gained a new dependency,flatmap-stream, that carried code to steal cryptocurrency wallets. Many of the affected projects did not list either package: both arrived as dependencies of other tools. - Their install scripts run with your permissions. A postinstall script in a transitive package runs on the developer's machine and in CI exactly like one in a direct dependency.
The practical defenses are a committed lockfile, a tool that checks the whole tree and not only the top level, and a look at what a lockfile change adds. Togoder Security reads the source of every package in a lockfile, direct and transitive: upload a lockfile to see the full list.
Frequently asked questions
What is the difference between a direct and a transitive dependency?
A direct dependency is one you list in your manifest (package.json, pyproject.toml, Cargo.toml, go.mod). A transitive dependency is one that a direct dependency, or one of its dependencies, needs. Both are installed and both run.
Does npm audit check transitive dependencies?
Yes. npm audit reads the whole tree from the lockfile, so it reports known vulnerabilities at any depth. It does not read the code, so a malicious transitive package with no advisory yet is reported as clean.
Are transitive dependencies of devDependencies a risk?
Yes. They are installed on developer machines and in CI, where tokens and credentials are, and their install scripts and build-time code run there even though they never reach production.