What is the difference between npm install and npm install –save

Posted on

In order to discuss the difference between the command of ‘npm install’ and the other command ‘npm install –save’, there are scenarios which has actually been executed. There are two scenarios as described below :

1. Installing package or module which has been actually defined in the file named ‘package.json’ located in the root folder of web-based application project.

2. Installing package or module which is not defined earlier in the file named ‘package.json’ as the file used to describe one of the attribute a project has which is the dependency package or module.

Basically, laying out two scenarios above is actually giving the answer of one aspect for each of the command’s difference. The first scenario where the package or module has already been defined in the ‘dependencies’ section, the correct one is to use the first command, ‘npm install’. It will automatically download all of the packages or modules specified in the folder named node_modules inside the root folder of the web-based application project. Either it has already been created or it is not created at all. If it is not been defined, the package or the module is still downloaded and stored in the node_modules folder, but it doesn’t add the name of the package or module into the package.json file. It is shown as follows :

user@hostname:~/nodejs/test$ npm install express
application-name@0.0.1 /home/user/nodejs/test
└── express@4.16.3  extraneous

user@hostname:~/nodejs/test$ vim package.json 
user@hostname:~/nodejs/test$

As it has been checked, the file named package.json before and after the execution, the entry of ‘express’ didn’t show up. If the file is not edited later on and it is kept as is without any action to add the name of the package or module in the package.json file, it will going to generate an error, especially when the ‘npm list’ command is executed. It is shown below :

user@hostname:~/nodejs/test$ npm list
application-name@0.0.1 /home/user/nodejs/test
...
├── express@4.16.3 extraneous
...

...
npm ERR! extraneous: express@4.16.3 /home/user/nodejs/test/node_modules/express
user@hostname:~/nodejs/test$

On the other hand, the second scenario is where the package or module has not been defined in the ‘dependencies’ section. Since it hasn’t been defined yet, in order to fill it into the ‘dependencies’ section without having to edit the ‘package.json’ file and add it in the ‘dependencies’ section, the command ‘npm install –save’ is executed. It is also created a new folder named node_modules if there isn’t any folder exist before to store the downloaded packages or modules.

The format of the command is shown as follows :

npm install --save package_module

So, the following is the execution of the command ‘npm install –save’ :

user@hostname:~/nodejs/test$ npm install --save express
application-name@0.0.1 /home/user/nodejs/test
└─┬ express@4.16.3 
  ├─┬ accepts@1.3.5 
  │ ├─┬ mime-types@2.1.18 
  │ │ └── mime-db@1.33.0 
  │ └── negotiator@0.6.1 
  ├── array-flatten@1.1.1 
  ├─┬ body-parser@1.18.2 
  │ ├── bytes@3.0.0 
  │ ├─┬ http-errors@1.6.3 
  │ │ └── inherits@2.0.3 
  │ ├── iconv-lite@0.4.19 
  │ └─┬ raw-body@2.3.2 
  │   └─┬ http-errors@1.6.2 
  │     ├── depd@1.1.1 
  │     └── setprototypeof@1.0.3 
  ├── content-disposition@0.5.2 
  ├── content-type@1.0.4 
  ├── cookie@0.3.1 
  ├── cookie-signature@1.0.6 
  ├─┬ debug@2.6.9 
  │ └── ms@2.0.0 
  ├── depd@1.1.2 
  ├── encodeurl@1.0.2 
  ├── escape-html@1.0.3 
  ├── etag@1.8.1 
  ├─┬ finalhandler@1.1.1 
  │ ├── debug@2.6.9 
  │ └── unpipe@1.0.0 
  ├── fresh@0.5.2 
  ├── merge-descriptors@1.0.1 
  ├── methods@1.1.2 
  ├─┬ on-finished@2.3.0 
  │ └── ee-first@1.1.1 
  ├── parseurl@1.3.2 
  ├── path-to-regexp@0.1.7 
  ├─┬ proxy-addr@2.0.3 
  │ ├── forwarded@0.1.2 
  │ └── ipaddr.js@1.6.0 
  ├── qs@6.5.1 
  ├── range-parser@1.2.0 
  ├── safe-buffer@5.1.1 
  ├─┬ send@0.16.2 
  │ ├── debug@2.6.9 
  │ ├── destroy@1.0.4 
  │ └── mime@1.4.1 
  ├── serve-static@1.13.2 
  ├── setprototypeof@1.1.0 
  ├── statuses@1.4.0 
  ├─┬ type-is@1.6.16 
  │ └── media-typer@0.3.0 
  ├── utils-merge@1.0.1 
  └── vary@1.1.2 

user@hostname:~/nodejs/test$ 

Below is the actual content of the package.json file before the execution of the command ‘npm install –save express’ :

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "nodejs ./bin/www"
  },
  "dependencies": {
    "static-favicon": "~1.0.0",
    "morgan": "~1.0.0",
    "cookie-parser": "~1.0.1",
    "body-parser": "~1.0.0",
    "debug": "~0.7.4",
    "jade": "~1.3.0"
  }
}

After the execution of the command ‘npm install –save express, below is the content of the file named ‘package.json’ :

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "nodejs ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.0.0",
    "cookie-parser": "~1.0.1",
    "debug": "~0.7.4",
    "express": "^4.16.3",
    "jade": "~1.3.0",
    "morgan": "~1.0.0",
    "static-favicon": "~1.0.0"
  }
}

If those two content is being compared, the different between before and after is clearly seen. The later one, has an entry added as follows :

"express": "^4.16.3",

So,basically, ‘npm install –save’ not only doing the same thing which is done when the ‘npm install’ is performed but also, in the context of the ‘npm install –save’ command execution, if the entry of that package or module hasn’t been defined in the ‘package.json’ file, it will also automatically add the entry to the ‘dependencies’ section.

So, in the end, it will not trigger any error when ‘npm list’ is executed. It means that the package or module has successfully downloaded and stored and on the other hand, it will kindly help us adding the entry to the package.json file in the ‘dependencies’ section. It will avoid error generated and it will also giving a help to declare it as part of the dependencies section which means it will be used in order to run the program or the application.


Leave a Reply