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.
4 Answers
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.

- 66,479
- 23
- 173
- 212
-
I've done the first option of table-flipping but when I run dep check it still shows a lot of "unused dependencies" that I definitely know I'm using. What is depcheck basing this off of? I also did `npm install -g depcheck react jsx` because I'm building that kind of app, but if I using es6, do I have include that too? what does it mean for it to have the syntax support for it? – stackjlei Sep 14 '16 at 00:25
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.

- 57,105
- 17
- 163
- 197
-
is there anyway to unflatten the dependency hierarchy so it just shows what i need? – stackjlei Sep 03 '16 at 06:51
-
1If you run the `npm list --depth=0` command, it should list only the root level modules. Typically, you will add modules as dependencies to your `package.json`, running `npm install` to install whatever you've added. If you need to know whether or not you've actually used/required a dependency, a tool like `depcheck` would be the way to go. – cartant Sep 03 '16 at 07:06
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.

- 762
- 7
- 18