简体   繁体   中英

npm install vs. edit package.json and npm update

Curious what is the difference between the two procedures:

  1. npm install xyz
  2. edit package.json , adding required module names like this:

"dependencies": {
    "express": "~3.4.4",
    "mongodb": "*",
    "body-parser": "*",
    "bson": "*"
  },

and then npm update

Basically package.json stores the dependencies of you application. Everything under "dependencies" is updated when you do npm update .

"bson": "*" means that it will update to latest version of module bson .

When you do npm install xyz you are basically installing xyz without telling package.json. Next time you do npm update npm will update everything under dependencies but not xyz

Here are the commands that will help you :

  1. npm install xyz This will install xyz without telling package.json .
  2. npm install --save xyz This will install xyz and also update package.json , so that when next time you do npm update it will update xyz as well.
  3. npm install This will install everything under dependencies in package.json .
  4. npm update This will update everything under dependencies in package.json .

If you just do npm install package , it doesn't add it to your package.json. Then, if you want to npm update or publish your package, it won't have all of the required packages.

You can also do npm install --save package , which will install and add to your package.json. ( see the docs )

Some answers here are outdated.

Quote from https://stackoverflow.com/a/19578808/1041641

As of npm 5.0.0, installed modules are added as a dependency by default, so the --save option is no longer needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM