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 [email protected] /home/user/nodejs/test └── [email protected] 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 [email protected] /home/user/nodejs/test ... ├── [email protected] extraneous ... ... npm ERR! extraneous: [email protected] /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 [email protected] /home/user/nodejs/test └─┬ [email protected] ├─┬ [email protected] │ ├─┬ [email protected] │ │ └── [email protected] │ └── [email protected] ├── [email protected] ├─┬ [email protected] │ ├── [email protected] │ ├─┬ [email protected] │ │ └── [email protected] │ ├── [email protected] │ └─┬ [email protected] │ └─┬ [email protected] │ ├── [email protected] │ └── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├─┬ [email protected] │ └── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├─┬ [email protected] │ ├── [email protected] │ └── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├─┬ [email protected] │ └── [email protected] ├── [email protected] ├── [email protected] ├─┬ [email protected] │ ├── [email protected] │ └── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├─┬ [email protected] │ ├── [email protected] │ ├── [email protected] │ └── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├─┬ [email protected] │ └── [email protected] ├── [email protected] └── [email protected] 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.