NPM Setup
npm is an essential tool in Node.js development. It helps developers install reusable packages, manage project dependencies, and execute application scripts.
In this lesson, we will learn how npm works, verify its installation, initialize a Node.js project, understand package.json, install a package, and execute a JavaScript program using npm.
1. Learning Objectives
After completing this lesson, you will be able to:
- Understand npm and its role in Node.js development.
- Verify the installed npm version.
- Initialize a project using npm.
- Understand package.json and package-lock.json.
- Install, use, and remove npm packages.
- Configure and execute npm scripts.
2. What Is npm?
npm stands for Node Package Manager. It is commonly used with Node.js to install and manage reusable JavaScript packages.
A package contains code that developers can reuse instead of implementing every feature from scratch. For example, a package may provide email validation, date formatting, logging, or other useful functionality.
Why Do We Use npm?
- To install packages required by an application.
- To manage project dependencies.
- To record package versions.
- To define reusable project commands.
- To help developers reproduce the project's dependency setup.
3. How npm Works
When we install a package, npm reads the project configuration, resolves the package dependencies, downloads the required package files, and updates the dependency information when appropriate.
Runs an npm command
Resolves packages and dependencies
Provides package information and files
Packages become available to the application
Package installation normally requires network access unless the required package data is already available locally or through an appropriately configured cache or registry.
4. Verify npm Installation
npm is generally installed along with Node.js when using the standard Node.js installer. We can verify it through the terminal.
Step 1: Open the Terminal
On Windows, open Command Prompt or PowerShell.
Step 2: Check the npm Version
npm --version
Example Output
10.x.x
The exact version depends on the installed npm release. If the command displays a version number, npm is accessible from the current terminal.
Check the Node.js Version Too
node --version
npm --version
Both commands should display version information when Node.js and npm are installed and available through the terminal.
5. Common npm Commands
npm provides commands for initializing projects, installing packages, removing dependencies, and running scripts.
| Command | Purpose |
|---|---|
npm --version |
Displays the npm version. |
npm init |
Starts the project initialization process. |
npm init -y |
Creates package.json using default answers. |
npm install |
Installs the project's declared dependencies. |
npm install package-name |
Installs a specified package. |
npm uninstall package-name |
Removes a package from the project. |
npm run |
Lists the available package scripts. |
6. Create a Node.js Project Using npm
Before installing packages, we should create a project folder and initialize it using npm.
Step 1: Create a Project Folder
Create a folder named NpmPractice. Open a terminal
in that folder.
Step 2: Initialize the Project
npm init -y
The -y option accepts the default answers to the
initialization questions. npm creates a package.json file.
Step 3: Understand the Project Structure
NpmPractice/
└── package.json
At this stage, the project contains its package configuration. We can now add JavaScript files and install packages as required.
7. What Is package.json?
package.json is the main project configuration file
used by npm. It contains information such as the project name,
version, dependencies, and scripts.
Example package.json
{
"name": "npm-practice",
"version": "1.0.0",
"description": "A beginner Node.js project",
"main": "app.js",
"scripts": {
"start": "node app.js"
}
}
Explanation of Properties
| Property | Explanation |
|---|---|
name |
Defines the project name. |
version |
Defines the project version. |
description |
Provides a short project description. |
main |
Specifies the package entry point in applicable package contexts. |
scripts |
Defines commands that can be executed through npm. |
8. Install an npm Package
npm allows us to add reusable functionality to our project.
In this example, we will install the validator
package, which provides validation functions.
Step 1: Install the Package
npm install validator
npm downloads the package and its required dependencies. It also records the dependency information in package.json and updates package-lock.json when appropriate.
Step 2: Understand the Structure
NpmPractice/
├── node_modules/
├── package.json
├── package-lock.json
└── app.js
-
node_modulescontains the installed package files. -
package.jsonrecords project configuration and dependencies. -
package-lock.jsonrecords the resolved dependency tree. -
app.jscontains the application's JavaScript code.
9. Use the Installed Package
After installing the package, create a file named
app.js in the project folder.
const validator = require("validator");
const email = "student@example.com";
if (validator.isEmail(email)) {
console.log("Valid email address");
} else {
console.log("Invalid email address");
}
Run the Program
node app.js
Expected Output
Valid email address
Program Explanation
-
require("validator")imports the installed package using CommonJS syntax. -
validator.isEmail(email)checks whether the supplied string has a valid email format. -
The
ifstatement checks the returned Boolean value. -
console.log()displays the result in the terminal.
10. Run Scripts Using npm
npm scripts allow developers to define reusable commands inside
the scripts section of package.json.
Example package.json
{
"name": "npm-practice",
"version": "1.0.0",
"scripts": {
"start": "node app.js",
"check": "node --version"
}
}
Execute the Start Script
npm start
npm executes the command configured under the
start script. In this example, it runs
node app.js.
Execute the Check Script
npm run check
This command runs node --version and displays
the active Node.js runtime version.
11. Common npm Problems
| Problem | Possible Reason | Solution |
|---|---|---|
npm is not recognized |
npm is missing or unavailable in PATH. | Verify the Node.js installation and reopen the terminal. |
package.json not found |
The command is running in the wrong directory. | Navigate to the project folder and check the file. |
Cannot find module |
The package is missing or cannot be resolved. | Check the package name and install the required dependency. |
| Network error | Internet, proxy, or registry connectivity problems. | Check the connection and npm configuration. |
| Invalid JSON | Incorrect syntax in package.json. | Check quotation marks, commas, and braces. |
Important Points
- Use
npm --versionto check npm. - Use
npm init -yto initialize a basic project. - Use
npm install package-nameto install a package. - Use package.json to manage project configuration.
- Use package-lock.json to record resolved dependency versions.
- Use npm scripts to execute reusable commands.
Practical Learning at CIIT TRAINING INSTITUTE
At CIIT TRAINING INSTITUTE, learners can practise npm commands and understand how real Node.js projects manage packages. These skills prepare learners for the next stages of backend development, including Express.js, REST APIs, databases, and authentication.
Skills Covered in This Lesson
- Verify npm installation.
- Initialize a Node.js project.
- Understand package.json and package-lock.json.
- Install and use an npm package.
- Configure and execute npm scripts.
Summary
npm helps developers install packages, manage dependencies, and execute project scripts. The package.json file stores project configuration, while package-lock.json records the resolved dependency tree. By practising npm commands and using packages in JavaScript programs, learners at CIIT TRAINING INSTITUTE can prepare for larger Node.js application development.