Introduction
This article will show how to install a package using npm. Not a normal package but it is a package for a development stage. The execution of the command to install it in this article will use Linux Ubuntu as an example. The version of the Linux Ubuntu is the ‘Linux Ubuntu 18.10’. But actually, any version will do as long as the ‘npm’ tool is available in the operating system. Just type the command ‘npm’ to test whether the tool exist or not. Actually, the installation of the package using npm is simple. In order to just install any available packages, just type the following command ‘npm i’ as it exist in this link. That link refer to the article with the title of ‘How to Install Package using npm via Command Line’. But to be more specific, just add an additional argument or parameter using the command pattern of ‘npm i -S package_name’. It exist in the article with the title of ‘How to Install Package using npm via Command Line’ in this link. What is the different between using ‘npm i -S package_name’ and the one available in this article.
The Difference between npm i -S and npm i -D
The following is the differentation of both of the command pattern :
Using the -S parameter as follows :
npm i -S package_name
For an example, let specify the package name is ‘mongoose’. The following is the command pattern :
npm i -S mongoose
So, the command execution will be in the following output :
user@hostname:~/express/myapp$ npm i -S mongoose npm WARN npm npm does not support Node.js v10.15.2 npm WARN npm You should probably upgrade to a newer version of node as we npm WARN npm can't make any promises that npm will work with this version. npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8, 9. npm WARN npm You can find the latest version at https://nodejs.org/ + [email protected] + [email protected] added 35 packages from 19 contributors in 8.741s user@hostname:~/express/myapp$
In this context, it will affect the ‘package.json’ file available in the application folder. This article use a generated application from the execution of Express Generator. One of the result is the existance of ‘package.json’ to specify the package dependencies of this application.
{ "name": "myapp", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www" }, "dependencies": { "cookie-parser": "~1.4.4", "debug": "~2.6.9", "ejs": "~2.6.1", "express": "~4.16.1", "http-errors": "~1.6.3", "mongoose": "^5.9.5", "morgan": "~1.9.1", "search": "^1.0.0", } }
The above is the content of file ‘package.json’ available inside the folder of an application generated using Express Generator. There is an additional line specifying ‘mongoose’ as a package dependency for the application.
Next is the one using the -D parameter as follows :
npm i -D dotenv
The following is the execution of the above command pattern :
user@hostname:~/express/myapp$ npm i -D dotenv npm WARN npm npm does not support Node.js v10.15.2 npm WARN npm You should probably upgrade to a newer version of node as we npm WARN npm can't make any promises that npm will work with this version. npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8, 9. npm WARN npm You can find the latest version at https://nodejs.org/ + [email protected] added 1 package in 1.807s user@hostname:~/express/myapp$
The result is very different. The ‘package.json’ file will have an additional line of configuration. Not in the ‘dependencies’ tag. But another tag below it. The following is the complete content of the ‘package.json’ after the above command execution :
{ "name": "myapp", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www" }, "dependencies": { "cookie-parser": "~1.4.4", "debug": "~2.6.9", "ejs": "~2.6.1", "express": "~4.16.1", "http-errors": "~1.6.3", "mongoose": "^5.9.5", "morgan": "~1.9.1", "search": "^1.0.0", }, "devDependencies": { "dotenv": "^8.2.0", } }