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.
Print the full dependency tree
For Maven:
For Gradle:
For npm:
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:
For Gradle:
For npm:
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:
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.
Related reading
- How can I use NSError in my iPhone App?
- How can I verify if compression is working correctly in Kafka 0.8.2.2?
- How can I view live MySQL queries?
- How can I write a `try`/`except` block that catches all exceptions?
- how can you easily check if access is denied for a file in .NET?
- How change topic leader or remove partition after some broker down?
- How come kafka fails to commit offset for a particular partition?
- How do I assert my exception message with JUnit Test annotation?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.