Michael Withington
Michael Withington

Reputation: 11

Why is my "npm install" downloading a lot of excess libraries that I did not specify?

This is my package.json dependencies array:

{
  "cookie-parser": "~1.0.1",
  "body-parser": "~1.0.0",
  "express": "~3.5.0",
  "socket.io":"1.0",
  "mongodb":"2.2.21",
  "request":"2.79.0", 
  "q":"1.4.1", 
  "bcryptjs":"2.4.0", 
  "jsonwebtoken":"7.2.1",
  "lodash":"4.17.4",
  "express-jwt": "5.1.0"
}

and then when i perform an NPM install this is the output: https://i.sstatic.net/tVUVz.jpg

Why am I getting all of this? On one PC I'm only getting the dependencies I requested but on my laptop I get all of this. I am very confused. I have the output as well but it looks ugly when pasted here.

Upvotes: 0

Views: 910

Answers (3)

revelt
revelt

Reputation: 2400

To dispel any weirdness regarding packages (which happens to me too), it's best to delete node_modules folder and npm i again. After that, combination of dependencies in your package.json (both dev-dependencies and normal-ones), should match the output of the command: npm ls --depth="0".

Now, on both your PC and your laptop the npm ls --depth="0" will match the combined list of all dependencies in package.json. Both cannot mismatch, otherwise that would mean npm became self-conscious and went rogue 😁.

Now, the readers of SO down-voted your question assuming you don't understand that dependencies can have dependencies and that's where the folders are coming from.

I hope that's not the case and that you will not get discouraged by any negativity happening in SO.

There is another possible scenario which might explain your case.

If you forgot to add --save when installing dependencies, dependencies were just saved into node_modules folder, but not added to your package.json. Thus, you might have inadvertently installed more things that you use.

When you delete the node_modules folder and install again, all dependencies are wiped and only what was in package.json is installed. That's why I suggest to delete node_modules folder first. As a note, npm-publishing automation libraries like my preferred np tend to wipe and reinstall dependencies before publishing the npm packages because of this exact reason. There can be "rubbish" in node_modules and wipe/reinstall helps.

I suggest to refer to this brilliant answer on --save.

With regards of previous answers, Jonathan mentioned npm ls but I find it unwieldy on anything else than trivial libraries — too much scrolling. It's best to use npm ls --depth="0" to check just the topmost level of your dependencies.

Upvotes: 1

Jorge Aguirre
Jorge Aguirre

Reputation: 2857

The packages you're installing have dependencies - this means that when you install them, you're also installing their dependencies.

Upvotes: 3

Keenan
Keenan

Reputation: 369

I went ahead and created a mock package.json with your dependencies and did an npm install.

Indeed, the packages actually displayed were the ones listed, but an npm ls will show all of the dependencies each of those dependencies installed, as noted by Pineda in a comment on your question.

You can use npm ls to view the dependency structure. Full documentation can be found here: https://docs.npmjs.com/cli/ls

Upvotes: 1

Related Questions