# Dead Dependencies & Bundle Weight

> As projects grow, dependencies accumulate. Tools like `depcheck` identify packages you installed but no longer use, while tools like Bundlephobia help you measure the real-world cost of a package before installing it.

Subject: Package Managers | Lesson 24 of 24 | 5 min read
Interactive lesson: https://codewithanimation.in/package-managers/depcheck-and-bundle-size
Previous: [Essential npm CLI Commands](https://codewithanimation.in/package-managers/npm-cli-toolkit.md)

## What is it?

Dependency-analysis tools compare declared packages with detected source, script and configuration uses. Reports are candidates for investigation, because tooling and dynamic references can produce false positives. Depcheck is no longer actively maintained.

A package's minified/gzipped estimate is useful for comparison, but it is not the exact cost in your application. Imports, shared dependencies, bundler configuration and tree shaking affect the production output.

## Why use it?

Dead dependencies bloat your `node_modules` folder, increase CI build times, and expand your attack surface for security vulnerabilities, all for code you aren't even using.

Installing massive libraries (like the original `moment.js`) can add hundreds of kilobytes to your frontend bundle, destroying your app's load time and Web Vitals.

## How it works

Review a dependency-analysis report against actual source, scripts, configuration and dynamic references before removing anything. Check tool maintenance and framework support; do not treat a reported unused package as an automatic deletion instruction.

Before installing a new package, go to bundlephobia.com or use `npx bundle-phobia-cli <pkg>` to check its minified + gzipped size.

## Common mistake: The Tree-Shaking Myth

Bundlers may need to retain modules with required side effects, but that does not imply every export or the entire package is unconditionally retained. Actual elimination depends on the module structure, metadata and production optimization.

## Tip: Check for Side-Effects-Free Packages

sideEffects metadata helps supporting bundlers decide whether unused modules can be skipped. Incorrectly marking CSS or initialization modules as side-effect-free can remove required behavior; validate the actual production output.

## Try it: Interactive Demo

Explore the concept in action using the interactive visualization below.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/depcheck-and-bundle-size

## Check yourself

### Question 1

A dependency-analysis tool reports an unused package. What should you do before removing it?

- A. Delete every reported package without checking
- B. Check source, scripts, configuration and dynamic uses
- C. Assume the package must be malicious
- D. Remove package.json instead

Correct answer: B

Static analysis can miss indirect tooling or dynamic references. Depcheck itself is no longer actively maintained, so review findings rather than treating them as deletion instructions.

- Why not A: A report can contain false positives or miss framework-specific uses.
- Why not C: Unused status is not evidence of malicious intent.
- Why not D: Removing the manifest does not validate dependency usage.

### Question 2

Does a package's minified/gzipped size estimate equal its exact cost in your application?

- A. Yes; every application includes exactly the same bytes
- B. Yes; node_modules disk size is always the browser transfer size
- C. No; actual cost depends on imports, bundling and existing shared code
- D. No; packages can never affect application load cost

Correct answer: C

A standalone estimate helps compare packages. Measure your production output to assess tree shaking, shared dependencies and application-specific inclusion.

- Why not A: Bundling choices and imported APIs affect included code.
- Why not B: Installed disk content differs from the delivered bundle.
- Why not D: Included package code can increase delivered bytes.

### Question 3

What does sideEffects: false communicate to a supporting bundler?

- A. Every export is automatically removed even when used
- B. The package is guaranteed to contain no runtime bugs
- C. All CSS imports are always preserved regardless of metadata
- D. Unused modules may be skipped without preserving their evaluation effects

Correct answer: D

This metadata helps optimization; it is not a guarantee all code disappears. Incorrectly marking CSS or initialization modules can remove behavior the app needs.

- Why not A: Used code can still be required in the output.
- Why not B: Optimization metadata is not a correctness proof.
- Why not C: Incorrect side-effect metadata can cause needed CSS to be dropped.

## Remember this

- Treat unused-dependency reports as review candidates; account for scripts, tooling and dynamic references.
- Dead dependencies slow down CI builds and increase security risks.
- Use package-size estimates for comparison and measure the actual production bundle for application impact.
- sideEffects metadata guides optimization; it is not a guarantee that all code disappears or that required effects are preserved.
