NPM: Why is this package installed?
node.jsNpmnode.js Problem Overview
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.
node.js Solutions
Solution 1 - node.js
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>
└─┬ pug@2.0.4
└─┬ pug-code-gen@2.0.2
└─┬ constantinople@3.1.2
└─┬ babel-types@6.26.0
└─┬ babel-runtime@6.26.0
└── core-js@2.6.10
Solution 2 - node.js
As you mention, npm ls
shows packages and their dependencies:
> npm ls leveldown
appless@5.0.0 C:\Users\mikem\Code\appless
`-- @architect/architect@5.7.0
`-- dynalite@2.2.0
`-- UNMET OPTIONAL DEPENDENCY leveldown@4.0.2
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.
Solution 3 - node.js
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
Solution 4 - node.js
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
.
Solution 5 - node.js
If you can't find a require
or import
, try looking at child package.json
s 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
Solution 6 - node.js
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."