Olivia Witt
Olivia Witt

Reputation: 953

NPM: Why is this package installed?

How do I determine why a particular package is installed? In other words, what package(s) depend on this package?

The package in question is babelify. npm ls shows it at the top level, but it is not included in package.json anywhere.

Upvotes: 95

Views: 34263

Answers (8)

Jean-Samuel Girard
Jean-Samuel Girard

Reputation: 597

Another useful tool for viewing dependencies : https://npmgraph.js.org/

Upvotes: 0

Pavel
Pavel

Reputation: 2564

Another solution is npm-dependency-inspector.

It is tool for inspecting package-lock.json file to determine, why some package installed.

Upvotes: 0

mikemaccana
mikemaccana

Reputation: 123590

As you mention, npm ls (short for 'list') shows packages and their dependencies:

> npm ls leveldown
[email protected] C:\Users\mikem\Code\appless
`-- @architect/[email protected]
  `-- [email protected]
    `-- UNMET OPTIONAL DEPENDENCY [email protected]

If npm ls shows it at the top level, and it is not a dependency in the top level package.json, it's likely was previously required and is now no longer used.

Use npm prune to remove the unused package.

Upvotes: 24

Ben Stickley
Ben Stickley

Reputation: 2120

npm explain <package name> is what you're looking for. It explains why a package is in your node_modules folder by showing a "bottoms up" view. See docs here

Upvotes: 29

musicin3d
musicin3d

Reputation: 1036

My one-liner, based on other answers: npm ls | grep -C 10 PACKAGE

Replace PACKAGE with the package you're looking for. This is simpler and faster than other suggestions. The shell is your friend, friends!

Breakdown/Explanation

  • npm ls - As explained above, this prints the dependency tree representation of your app.
  • | - The secret sauce of *nix shells. This sends the output to the next program, instead of printing it.
  • grep [...] PACKAGE - Search for the string "PACKAGE" (This is actually regex, but that's irrelevant.)
  • -C 10 - This tells grep to print 10 extra lines around the matching lines. Without this, grep would only print the lines were PACKAGE was found. Increasing this number gives you more context. If -C doesn't work on your system (less common linux versions), try using -B 10 -A 10 instead. It's a more verbose way of doing the same thing: "10 lines before, 10 lines after."

Upvotes: 1

Khaled Awad
Khaled Awad

Reputation: 1834

Use npm ls to list installed packages and see the dependency graph of a given package eg:

> npm ls core-js

my_module /path/to/my_module>
└─┬ [email protected]
  └─┬ [email protected]
    └─┬ [email protected]
      └─┬ [email protected]
        └─┬ [email protected]
          └── [email protected]

Upvotes: 108

Dheeraj Vepakomma
Dheeraj Vepakomma

Reputation: 28767

There's a module called npm-why which identifies why a package has been installed.

Of course, if you're using yarn, you have a built-in command yarn why.

Upvotes: 10

JL Peyret
JL Peyret

Reputation: 12204

If you can't find a require or import, try looking at child package.jsons to see who else needs it.

(Note: find requires Linux/macOS, this won't work on Windows)

find . -name package.json -exec grep -l babelify /dev/null {} \;

./node_modules/browserify-zlib/package.json
./node_modules/cssnext/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/reporter/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/node_modules/@cypress/browserify-preprocessor/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/node_modules/async/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/node_modules/babel-core/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/node_modules/babelify/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/node_modules/getos/node_modules/async/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/node_modules/object-assign/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/node_modules/watchify/node_modules/browserify-zlib/package.json
./node_modules/cypress/dist/Cypress.app/Contents/Resources/app/packages/server/package.json
./node_modules/eslint/package.json
./node_modules/extract-text-webpack-plugin/node_modules/async/package.json
./node_modules/getos/node_modules/async/package.json
./node_modules/postcss-modules-extract-imports/package.json
./node_modules/postcss-modules-scope/package.json
./node_modules/webpack/node_modules/async/package.json

Upvotes: 3

Related Questions