Chapter 3
Chapter 3
Introduction to npm : Npm stands for Node Package Manager. It is a package manager for the Node
JavaScript platform. Npm is known as the world’s largest software registry. Open-source developers all over
the world use npm to publish and share their source code.
● The website allows you to find third-party packages, set up profiles, and manage your packages.
● The command-line interface or npm CLI that runs from a terminal to allow you to interact with npm.
● The registry is a large public database of JavaScript code.
npm is free to use. You can download all npm public software packages without any registration or logon.
Command Line Client : npm includes a CLI (Command Line Client) that can be used to download and
install software:
Windows Example
C:\>npm install <package>
To find the npm CLI on your computer, you run the npm command from a terminal:
npm
For example, the following command will display the current npm version on your system:
npm -v
Software Package Manager
The name npm (Node Package Manager) stems from when npm first was created as a package manager for
Node.js.
At least two fields must be present in the definition file: name and version.
package.json : In general, every npm project has a file called package.json located in the root directory.
The package.json is a plain text file that contains important information that npm uses to identify the project
and handle dependencies. To create the package.json file, you go to the root directory of the project and
execute the following command:
npm init
When you run the npm init command, it will prompt you for the project information including:
● Package name
● Version
● Test command
● Git repository
● Keywords
● Author
● License
If you hit Return or Enter, it will accept the default values and move on to the next prompt.
If you want to use default options, you use the following command:
To save some typing, you can use a shorter version of the npm install command:
npm i <package_name>
Code language: JavaScript (javascript)
there are several ways to run npm commands. Below are the brief lists of some of the commonly used npm
commands (or aliases).
What is a Package?
A package in Node.js contains all the files you need for a module.
Modules are JavaScript libraries you can include in your project.
Download a Package
Open the command line interface and tell NPM to download the package you want.
Download "upper-case":
NPM creates a folder named "node_modules", where the package will be placed. All packages you install in
the future will be placed in this folder.
C:\Users\My Name\node_modules\upper-case
Using a Package
Include the "upper-case" package the same way you include any other module:
var uc = require('upper-case');
Create a Node.js file that will convert the output "Hello World!" into upper-case letters:
var uc = require('upper-case');
res.write(uc.upperCase("Hello World!"));
res.end();
}).listen(8080);
Save the code above in a file called "demo_uppercase.js", and initiate the file:
Initiate demo_uppercase:
C:\Users\Your Name>node demo_uppercase.js
If you have followed the same steps on your computer, you will see the same result as the
example: http://localhost:8080
Express:
Express is a minimal and flexible Node.js web application framework that provides a robust set of features
to develop web and mobile applications. It facilitates the rapid development of Node based Web
applications. Following are some of the core features of Express framework −
● Allows to set up middlewares to respond to HTTP Requests.
● Defines a routing table which is used to perform different actions based on HTTP Method and URL.
● Allows to dynamically render HTML Pages based on passing arguments to templates.
Express.Js
Advantages of Express.js
Installing Express
Firstly, install the Express framework globally using NPM so that it can be used to create a web application
using node terminal.
$ npm install express --save
The above command saves the installation locally in the node_modules directory and creates a directory
express inside node_modules. You should install the following important modules along with express −
● body-parser − This is a node.js middleware for handling JSON, Raw, Text and URL encoded form
data.
● cookie-parser − Parse Cookie header and populate req.cookies with an object keyed by the cookie
names.
● multer − This is a node.js middleware for handling multipart/form-data.
$ npm install body-parser --save
$ npm install cookie-parser --save
$ npm install multer --save
Hello world Example : Following is a very basic Express app which starts a server and listens on port 8081
for connection. This app responds with Hello World! for requests to the homepage. For every other path, it
will respond with a 404 Not Found.
Web Server : First of all, import the Express.js module and create the web server as shown below.
app.js: Express.js Web Server
Example :
Express.js
Web Application