What is node js?
Node.js can be used to build different types of applications such as command line application, web application, real-time chat application, REST API server etc. However, it is mainly used to build network programs like web servers, similar to PHP, Java, or ASP.NET.
Node.js was written and introduced by Ryan Dahl in 2009.
Advantages of Node.Js
Node.js is an open-source framework under MIT license. (MIT license is a free software license originating at the Massachusetts Institute of Technology (MIT).)
Lightweight framework that includes bare minimum modules. Other modules can be included as per the need of an application.
Asynchronous by default. So it performs faster than other frameworks.
Cross-platform framework that runs on Windows, MAC or Linux
Node package manager
Node Package Manager (NPM) is a command line tool that installs, updates or uninstalls Node.js packages in your application. It is also an online repository for open-source Node.js packages. The node community around the world creates useful modules and publishes them as packages in this repository.
NPM is included with Node.js installation. After you install Node.js, verify NPM installation by writing the following command in terminal or command prompt.
C:\> npm -v
NPM performs the operation in two modes: global and local. In the global mode, NPM performs operations which affect all the Node.js applications on the computer whereas in the local mode, NPM performs operations for the particular local directory which affects an application in that directory only.
Install package locally
Use the following command to install any third party module in your local Node.js project folder.
C:\>npm install <package name>
For example, the following command will install ExpressJS into MyNodeProj folder.
C:\MyNodeProj> npm install express
All the modules installed using NPM are installed under node_modules folder. The above command will create ExpressJS folder under node_modules folder in the root folder of your project and install Express.js there.
Add dependency into package.json
Use --save at the end of the install command to add dependency entry into package.json of your application.
For example, the following command will install ExpressJS in your application and also adds dependency entry into the package.json.
C:\MyNodeProj> npm install express --save
The package.json of NodejsConsoleApp project will look something like below.
Install package globally
NPM can also install packages globally so that all the node.js application on that computer can import and use the installed packages. NPM installs global packages into /<User>/local/lib/node_modulesfolder.
Apply -g in the install command to install package globally. For example, the following command will install ExpressJS globally.
C:\MyNodeProj> npm install -g express
To update the package installed locally in your Node.js project, navigate the command prompt or terminal window path to the project folder and write the following update command.
C:\MyNodeProj> npm update <package name>
The following command will update the existing ExpressJS module to the latest version.
C:\MyNodeProj> npm update express