stackjlei
stackjlei

Reputation: 10035

How do I check node_modules directory for unnecessary packages?

My node_modules has packages that are not listed in my package.json's dependencies, so I'm guessing that those packages are dependencies of my dependencies. How would I be able to check this? I want to make sure that there aren't any unnecessary packages in my node_modules directory.

Upvotes: 0

Views: 3778

Answers (4)

Brandon Parker
Brandon Parker

Reputation: 803

Since packages can require other packages, just because there are packages in the node_modules folder that don't exist in your packages.json file doesn't mean they aren't needed by one of your specified packages.

If you run an npm prune command on the root directory of your solution it will read the dependency tree and remove the packages that are truly no longer needed.

Upvotes: 1

Trott
Trott

Reputation: 70085

If your dependency list won't take too long to reinstall, a simple option is a table-flip: remove the node_modules directory entirely and run npm install to re-create it.

If you don't want to do that, you can try tools that inspect your dependencies, like depcheck as @sagar-gopale suggests in their answer.

Related: Run npm -v to find out if you are running npm v2 or v3. Like @cartant says in their answer, with v3, your node_modules directory will be maximally flat, which means things that used to appear as subdirectories of other modules (when installed with npm v2) will now appear at the top level of node_modules itself. That may be the reason you see more modules than you expect.

Upvotes: 2

XCEPTION
XCEPTION

Reputation: 1753

Please checkout this package. https://www.npmjs.com/package/depcheck

Upvotes: 1

cartant
cartant

Reputation: 58400

If you are using NPM 3, you will likely see a large number of modules that you were not expecting to see in the node_modules directory, as NPM 3 flattens the dependency hierarchy.

Whichever version you are using, if you run the npm list command, NPM should highlight any extraneous modules that are not required.

Upvotes: 1

Related Questions