JavaScript applications often start with a small number of dependencies. A developer installs a framework, adds a few utility packages, and begins building features. Over time, the project grows. New libraries are introduced, old features are removed, and different developers add packages to solve specific problems.

The dependency list can gradually become much larger than the application actually needs.

For example, a project might still contain a date-handling library that was replaced several months ago. Another package may have been installed for a feature that was never completed. Some dependencies may also remain after a migration from one framework or tool to another.

These packages are easy to overlook because an application can continue working even when they are no longer required.

Unused dependencies create unnecessary maintenance work, increase installation time, and may introduce additional security and licensing considerations. They can also make it harder to understand which packages are genuinely important to the application.

The challenge is identifying unused dependencies accurately. Searching for package names in source files is not enough because JavaScript projects can reference packages through configuration files, build scripts, plugins, dynamic imports, and framework conventions.

This article explains how to identify unused JavaScript dependencies, use automated analysis tools, investigate false positives, and safely remove packages from an application.

Why Unused Dependencies Matter

Keeping unnecessary packages in a project may not cause an immediate failure, but it can create problems over time.

Larger dependency trees

Every installed package may bring additional packages with it.

For example, a project might directly depend on a library that requires several other packages. Even if the application no longer uses the original library, those transitive dependencies may still be installed.

Removing an unnecessary direct dependency can therefore reduce the size of the dependency tree.

Longer installation times

Package managers need to resolve and install the dependencies declared by the project.

When unnecessary packages remain in package.json, they continue to participate in dependency installation and resolution.

This can increase installation work, especially in large applications and monorepos.

More security alerts

Security scanners examine installed packages and their dependency trees.

An unused package can still appear in vulnerability reports. Developers may spend time investigating and updating a dependency that the application no longer needs.

Removing unnecessary packages reduces the number of dependencies that need to be reviewed.

It does not automatically make the application secure, but it helps reduce avoidable dependency exposure.

More complicated upgrades

When a project contains many dependencies, upgrading the package ecosystem becomes more difficult.

An unnecessary package may introduce version constraints or conflicts with another library.

Keeping the dependency list focused makes upgrades easier to plan and review.

Understanding How Dependency Analysis Works

A dependency analysis tool does more than search for package names.

It generally examines project configuration, source files, imports, exports, scripts, and other references to understand how packages are used.

For example, consider the following package.json:

JSON
{
  "dependencies": {
    "react": "^19.0.0",
    "axios": "^1.7.0",
    "lodash": "^4.17.21",
    "moment": "^2.30.0"
  },
  "devDependencies": {
    "typescript": "^5.0.0",
    "eslint": "^9.0.0"
  }
}

Suppose the application imports React and Axios but no longer uses Lodash or Moment.

A dependency analysis tool may identify those two packages as unused.

However, the analysis needs to understand the project's entry points and configuration files. A package can be used without appearing in an ordinary source-code import statement.

For example, a package may be referenced in:

  • Build configuration files.
  • Test configuration files.
  • Package scripts.
  • Framework plugins.
  • CSS processing configuration.
  • Dynamic import expressions.
  • Files generated or loaded automatically by a framework.

This is why automated analysis should be treated as a starting point rather than an instruction to delete every reported package.

Using Knip to Find Unused Dependencies

Knip is a dependency analysis tool designed for JavaScript and TypeScript projects.

It can identify unused dependencies, unused files, unused exports, and dependencies that are referenced by the project but missing from package.json.

It also supports several popular frameworks and development tools through plugins.

Install Knip

For a project using npm, install Knip as a development dependency:

Bash
npm install --save-dev knip

For a project using pnpm:

Bash
pnpm add -D knip

For Yarn:

Bash
yarn add -D knip

Keeping the analysis tool in devDependencies makes it available to developers and CI pipelines without treating it as a runtime dependency.

Run the analysis

Execute Knip from the project root:

Bash
npx knip

If the project uses pnpm:

Bash
pnpm exec knip

Knip analyzes the project and reports findings such as unused dependencies, unused files, and unused exports.

To focus specifically on dependencies, use:

Bash
npx knip --dependencies

This makes the output easier to review when the immediate goal is cleaning up package.json.

Understand the results

Consider a simplified example:

Code
Unused dependencies

moment
lodash

This suggests that the analyzer could not find references to these packages in the project files it analyzed.

Before removing them, verify that the tool understands the application's entry points and configuration.

A package may appear unused because the analyzer cannot see how a framework or build tool loads it.

How Knip Builds a Dependency Graph

Understanding the analysis process helps developers interpret its results.

Knip starts from known entry files and follows imports to identify the files and packages reachable from those entry points.

For example, consider this project structure:

Code
src/
  index.ts
  app.ts
  services/
    user-service.ts
  utils/
    date-helper.ts
  legacy/
    old-report.ts

The application entry point might be index.ts.

It imports app.ts, which imports user-service.ts. The dependency graph therefore includes those files and the packages they reference.

If legacy/old-report.ts is not reachable from any recognized entry point, Knip may report it as an unused file.

Dependencies used only by that file may also appear unused.

This explains an important behavior: a missing entry point can produce several related findings.

For example, a test runner may load test files automatically. If those files are not recognized as entry points, packages used only by tests may appear unused.

The solution is to configure the analyzer correctly rather than immediately deleting the reported dependencies.

Configuring Knip for Your Project

Simple projects may work with the default configuration. Larger applications often need additional configuration.

Create a knip.json file in the project root:

JSON
{
  "$schema": "https://unpkg.com/knip@5/schema.json",
  "entry": [
    "src/index.ts",
    "scripts/**/*.ts",
    "vite.config.ts"
  ],
  "project": [
    "src/**/*.{js,jsx,ts,tsx}",
    "scripts/**/*.ts",
    "*.config.{js,ts}"
  ]
}

The entry property identifies files that act as starting points for the application or its tools.

The project property identifies files that should be analyzed as part of the project.

These patterns are examples. Adjust them to match the actual structure of your application.

Include test files

Test files often use dependencies that are not imported by production code.

For example:

Code
src/
  app.ts
  services/
    user-service.ts
  tests/
    user-service.test.ts

If the test runner automatically discovers files under the tests directory, include those files in the analysis configuration or use the appropriate Knip plugin.

This helps prevent test dependencies from being incorrectly reported as unused.

Include configuration files

Build tools and development frameworks frequently load packages through configuration files.

For example:

Code
vite.config.ts
eslint.config.js
playwright.config.ts

A package used only in one of these files may not appear in the application's normal import graph.

Make sure the relevant configuration files are recognized by the analyzer.

Knip includes plugins for many popular tools and frameworks. Use the available plugin support where appropriate instead of manually maintaining a large list of entry patterns.

Identifying Dependencies That Are Missing From package.json

Unused dependency detection is only one part of dependency analysis.

Another important problem occurs when an application imports a package without declaring it directly.

Consider this example:

JavaScript
import axios from "axios";

export async function getUsers() {
  const response = await axios.get("/api/users");
  return response.data;
}

If Axios is not listed in the project's package.json, the application may still work because another package has installed it as a transitive dependency.

However, relying on a transitive dependency directly is risky.

A future update may remove or change that dependency, causing the application to fail.

Knip can identify these unlisted dependencies.

For example, if the analysis reports Axios as an unlisted dependency, add it directly:

Bash
npm install axios

This makes the application explicitly responsible for the package it imports.

The same principle applies to development tools and command-line packages referenced by project scripts.

Understanding False Positives

Automated dependency analysis cannot always understand every way JavaScript applications load code.

A reported unused dependency may still be required.

Dynamic imports

Consider this example:

JavaScript
const moduleName = getModuleName();

const module = await import(`./modules/${moduleName}.js`);

The module name is determined at runtime.

A static analyzer may not know which module will be loaded because the import path is constructed dynamically.

If a package is loaded through similar logic, it may not be detected reliably.

Review dynamic loading code before removing reported dependencies.

Framework-generated imports

Some frameworks automatically discover files or load modules based on naming conventions.

For example, a framework may automatically load files from a particular directory without requiring explicit import statements.

If the analyzer does not understand that convention, it may report related files or dependencies as unused.

Use the appropriate framework plugin or configure the relevant entry files.

Conditional dependencies

A configuration file may load a package only when a particular environment variable is enabled.

For example:

JavaScript
const reporters = ["default"];

if (process.env.ENABLE_REPORTING) {
  reporters.push("custom-reporter");
}

The custom reporter may be required in a specific CI environment even if the condition is not enabled during local analysis.

Check environment-dependent configuration before removing related packages.

Public library exports

A project that publishes a reusable library has a different dependency analysis requirement from a private application.

An exported function may be consumed by external applications even when no file inside the repository imports it.

For example:

JavaScript
export function createClient() {
  return {};
}

If the package is intended for external consumers, the absence of internal imports does not mean the export is unnecessary.

Review the package's public API before removing exports or changing package entry points.

Using Dependency Analysis in a Monorepo

Monorepos contain multiple applications and libraries inside one repository.

A typical structure might look like this:

Code
workspace/
  apps/
    web/
      package.json
      src/
    admin/
      package.json
      src/
  packages/
    shared/
      package.json
      src/
    logging/
      package.json
      src/
  package.json

A dependency may be used by one workspace but not another.

For example, the web application may use a charting library while the admin application does not.

Running dependency analysis only at the repository root may not provide enough information about each workspace.

Knip supports workspace analysis and can inspect dependencies across a monorepo.

Start with:

Bash
npx knip

Review the results by workspace rather than treating the repository as one application.

Pay attention to dependencies declared at the root but used only by a child workspace. Also verify that packages shared between workspaces are represented correctly.

A package should not be removed from the root simply because the root workspace does not import it directly.

Safely Removing Unused Dependencies

Once the analysis is complete, remove packages in small groups.

Suppose the analyzer reports that Moment is unused.

Remove it using the package manager:

Bash
npm uninstall moment

For pnpm:

Bash
pnpm remove moment

For Yarn:

Bash
yarn remove moment

The package manager updates package.json and the lockfile.

After removal, run the project's tests and build:

Bash
npm test
npm run build

Then verify that a clean installation works:

Bash
npm ci

If the project uses pnpm, use the corresponding clean-install workflow:

Bash
pnpm install --frozen-lockfile

Review the resulting changes before committing them.

Do not remove a large number of packages at once without testing. If something breaks, smaller changes make it easier to identify the dependency responsible.

Automating Dependency Checks in CI

Dependency analysis becomes more useful when it is part of the development workflow.

Add a script to package.json:

JSON
{
  "scripts": {
    "check:dependencies": "knip --dependencies"
  }
}

Developers can now run:

Bash
npm run check:dependencies

You can also add the check to a CI workflow:

SQL
name: Dependency Analysis

on:
  pull_request:
  push:
    branches:
      - main

jobs:
  dependencies:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Install dependencies
        run: npm ci

      - name: Check dependencies
        run: npm run check:dependencies

This example assumes Knip is already listed in the project's development dependencies.

A CI check can help identify new dependency issues before they become part of the main branch.

For larger projects, begin with reporting and review the results. Introduce stricter enforcement after the configuration has been validated.

Common Mistakes to Avoid

Removing packages based only on search results

A package may be loaded through configuration, dynamic imports, or framework conventions.

Always investigate how the package is used before removing it.

Ignoring missing dependencies

An application can work because a package is installed indirectly through another dependency.

If the application imports that package directly, declare it explicitly.

Ignoring configuration hints

Analysis tools may report configuration problems that affect the accuracy of their results.

Resolve missing entry points and unsupported file patterns before trusting the unused dependency report.

Using outdated analysis tools without checking their maintenance status

Older tools may not understand modern JavaScript frameworks, TypeScript patterns, or workspace configurations.

Depcheck was widely used for dependency analysis, but its maintainers now describe it as no longer actively maintained and recommend Knip as an alternative.

Choose tools that support the project's current framework and build setup.

Automatically removing every reported package

Automatic fixes can save time, but they should not replace code review.

Use version control, inspect the proposed changes, and run the application's tests before accepting them.

Best Practices for Managing JavaScript Dependencies

A clean dependency list requires regular attention rather than a one-time cleanup.

Consider these practices:

  • Run dependency analysis before major upgrades.
  • Include application entry points, test files, and configuration files.
  • Use framework plugins where available.
  • Review unused dependency reports before removing packages.
  • Declare packages that the application imports directly.
  • Analyze each workspace separately in a monorepo.
  • Keep runtime dependencies separate from development dependencies.
  • Run tests and production builds after dependency changes.
  • Add dependency analysis to CI once the configuration is reliable.
  • Review automated fixes through version control.

These practices help teams maintain a dependency list that reflects how the application actually works.

Conclusion

Unused JavaScript dependencies are easy to overlook, especially in applications that have been maintained for several years or developed by multiple teams.

They can increase installation work, complicate upgrades, and create unnecessary security and licensing reviews.

Tools such as Knip help developers understand which dependencies are actually referenced by the application. However, accurate results depend on recognizing the project's entry points, configuration files, framework conventions, and dynamic loading behavior.

Start by analyzing the project, review the reported findings, and remove dependencies in controlled steps. Verify every change with clean installations, tests, and production builds.

Regular dependency analysis makes package management easier and helps keep JavaScript applications simpler, more predictable, and easier to maintain.