transitive dependencies
dependency management
software development
package management
debugging

How to find where a transitive dependency comes from (Maven, Gradle, npm)

Codemia Knowledge Hub ·

Ask your build tool for the resolved dependency tree and follow the path from your project to the package in question. A transitive dependency is one you never declared yourself but receive through one of your direct dependencies, and the tree shows exactly which direct dependency brought it in. You usually need this when chasing version conflicts, unexpected vulnerabilities, oversized builds, or a library you never meant to include.

For Maven:

bash
mvn dependency:tree

For Gradle:

bash
./gradlew dependencies

For npm:

bash
npm ls package-name

Each of these shows which direct dependency pulled in the transitive one.

Filter the tree to one package

Large graphs are hard to read, so narrow the output to the library you care about.

For Maven:

bash
mvn dependency:tree -Dincludes=org.example:problem-lib

For Gradle:

bash
./gradlew dependencyInsight --dependency problem-lib --configuration runtimeClasspath

For npm:

bash
npm ls problem-lib

This answers the real question most directly: why is this specific library here?

Decide why the package is a problem

Before excluding or overriding anything, be clear on what is actually wrong. Typical reasons:

  • a version conflict with another library
  • an unexpected license or vulnerability report
  • a huge artifact size
  • behavior differences caused by a newer or older transitive version

The reason determines the fix. Sometimes exclusion is right, sometimes version alignment is better, and sometimes the dependency is harmless and only looked suspicious in a report.

Read the whole path, not just the parent

A transitive dependency can arrive through several layers. Your project might depend on A, which depends on B, which depends on C, and C is the package you are investigating.

The best place to fix it may not be the immediate parent. Upgrading or replacing A can be cleaner than manually excluding C further down.

Exclude, pin, upgrade or replace

Once you know the source, the usual options are:

  • exclude the transitive dependency
  • force a specific version
  • upgrade the direct dependency that brings it in
  • replace the direct dependency entirely

A Maven exclusion looks like this:

xml
1<dependency>
2  <groupId>com.example</groupId>
3  <artifactId>app-lib</artifactId>
4  <version>1.0.0</version>
5  <exclusions>
6    <exclusion>
7      <groupId>org.example</groupId>
8      <artifactId>problem-lib</artifactId>
9    </exclusion>
10  </exclusions>
11</dependency>

Exclusions are only safe if you understand what functionality the removed library was providing.

Recheck the graph and run the tests

After an exclusion or version override, inspect the tree again. Removing one path often reveals that the same library still arrives through another branch. Different configurations (for example compile versus runtime classpaths) can also resolve differently, so check the one that matters.

Then run your tests. Dependency changes often compile cleanly but fail at runtime when some code path expects the removed library, or when a forced version is not compatible.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions