Lesson 5 of 50 – NPM
10%
10%

NPM in Node.js

NPM stands for Node Package Manager. It is commonly used to install, manage, and share packages that developers use in Node.js projects.

Packages provide reusable code that helps developers build applications without writing every feature from scratch. For example, Express.js helps us create web servers and APIs.

Remember: NPM is installed with the standard Node.js installer. You can use the npm command from your terminal.

What Is NPM?

NPM is the package manager commonly used in Node.js projects. It provides a command-line tool and an online package registry containing reusable JavaScript packages.

Important Parts of NPM
  • CLI: The command-line tool used to run NPM commands.
  • Registry: An online collection of packages.
  • Packages: Reusable code that can be installed in projects.
  • package.json: A file that stores project information and package configuration.

Why Do We Use NPM?

1. Install Packages

Install packages created by other developers instead of implementing every feature yourself.

2. Manage Dependencies

Keep track of the packages your application requires.

3. Run Project Scripts

Define reusable commands in package.json, such as commands to start an application or run tests.

4. Publish Packages

Developers can publish reusable packages to the NPM registry.

Step 1: Check the NPM Version

First, make sure Node.js and NPM are installed on your computer. Open Command Prompt or a terminal and enter:

Command
npm -v
Example Output
Terminal Output
10.9.2

This is an example version number. Your installed version may differ.

Step 2: Create a Project Folder

Create a folder named MyNpmProject and open it in your code editor.

Project Folder
MyNpmProject/

Step 3: Initialize an NPM Project

Before installing project dependencies, you can initialize your project with the following command:

Initialize Project
npm init

NPM asks questions about your project, such as its name, version, description, and entry point. Your answers are used to create a package.json file.

Quick Initialization

To create a default package.json without answering each question, use:

Command
npm init -y
Example Project Structure
Files and Folders
MyNpmProject/
└── package.json

What Is package.json?

The package.json file contains information about your project, including its name, version, scripts, and dependencies.

Example: package.json
{
  "name": "my-npm-project",
  "version": "1.0.0",
  "description": "My first NPM project",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  }
}

This is a simplified example. NPM can create a similar file automatically when you initialize a project.

Step 4: Install a Package

Let's install a package named lodash. Lodash provides utility functions for working with JavaScript values and collections.

Install Lodash
npm install lodash

After installation, your project will normally contain:

Project Structure
MyNpmProject/
├── node_modules/
├── package.json
└── package-lock.json
  • node_modules/ contains installed packages and their dependencies.
  • package.json records project details and dependency declarations.
  • package-lock.json records the resolved dependency versions for reproducible installations.
Important: You normally should not edit files inside node_modules directly. Use NPM commands to manage your packages.

Step 5: Use an Installed Package

After installing Lodash, create a file named app.js in your project folder.

app.js
const _ = require("lodash");

const numbers = [10, 20, 30, 40];

console.log(_.sum(numbers));
Run the Program
node app.js
Output
Terminal Output
100
Explanation
  • require("lodash") loads the installed Lodash package in this CommonJS example.
  • numbers stores an array of numbers.
  • _.sum(numbers) calculates the sum of the array values.
  • console.log() displays the result.

Step 6: Install a Development Dependency

Some packages are needed during development but not when the application is running in production. These can be installed as development dependencies.

Install Nodemon
npm install --save-dev nodemon

Nodemon can restart a Node.js application when it detects file changes. It is commonly used during development.

Step 7: Uninstall a Package

If you no longer need a package, you can remove it from your project.

Uninstall Lodash
npm uninstall lodash

NPM removes the package and updates the project's dependency information.

Useful NPM Commands

NPM Command Reference
Command Purpose
npm -v Displays the NPM version.
npm init Creates a project configuration interactively.
npm init -y Creates a default package.json.
npm install lodash Installs Lodash in the project.
npm install Installs dependencies declared by the project.
npm uninstall lodash Removes Lodash from the project.
npm list Lists installed packages for the project.
npm run start Runs the configured start script.

Practical Assignment

Practice Tasks
  1. Check your NPM version using npm -v.
  2. Create a folder named MyNpmProject.
  3. Open a terminal in that folder.
  4. Run npm init -y.
  5. Install Lodash using npm install lodash.
  6. Create app.js and calculate the sum of an array using Lodash.
  7. Run your program using node app.js.
  8. Install Nodemon as a development dependency.
  9. Practice removing a package using npm uninstall.
💡 Remember
NPM helps you install and manage reusable packages. The package.json file describes your project and its dependencies.

🧠 Quiz

1. What is the full form of NPM?

2. Which file stores project dependency information?

3. Which command installs a package named Lodash?