Node.js Tutorial – Day 6: NPM (Node Package Manager) and Package Management
4 mins read

Node.js Tutorial – Day 6: NPM (Node Package Manager) and Package Management

Welcome back to our Node.js Tutorial series!
So far, we’ve covered:

Today in Day 6, we’ll explore NPM (Node Package Manager), one of the most important tools in Node.js development.


What is NPM?

NPM is the default package manager for Node.js. It comes bundled with Node.js installation and allows developers to:

  • Install libraries (packages/modules) from a global registry.
  • Manage dependencies in projects.
  • Run scripts for automation (build, test, start).
  • Share custom packages with others.

👉 In short, NPM is like an app store for JavaScript packages.


Checking NPM Installation

If Node.js is installed, NPM comes with it. To check:

npm -v

Example output:

10.5.0

If you see a version number, NPM is installed successfully.


Initializing a New Project with NPM

To create a new Node.js project, we first initialize NPM.

mkdir my-app
cd my-app
npm init -y

This will generate a package.json file:

{
  "name": "my-app",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT"
}

The package.json file is the blueprint of your project, storing metadata and dependencies.


Installing Packages with NPM

1. Installing a Package (Local)

npm install lodash

This installs the lodash library inside node_modules/ and updates package.json.

2. Installing a Package Globally

npm install -g nodemon

This installs the package system-wide, accessible from anywhere.

3. Development Dependency

npm install jest --save-dev

Adds the package only for development (e.g., testing, linting).


Using Installed Packages

Example with Lodash:

const _ = require('lodash');

const numbers = [1, 2, 3, 4, 5];
console.log("Shuffled Numbers:", _.shuffle(numbers));

Output:

Shuffled Numbers: [3, 5, 1, 4, 2]

Updating and Removing Packages

  • Update a package
npm update lodash
  • Uninstall a package
npm uninstall lodash
  • Check outdated packages
npm outdated

NPM Scripts

You can add custom scripts in package.json:

"scripts": {
  "start": "node index.js",
  "dev": "nodemon index.js",
  "test": "jest"
}

Now run:

npm run dev

This makes development workflows faster.


Mini-Project: Weather App with NPM

Let’s create a small Weather App using an external package (axios) to fetch data.

Step 1: Initialize project

mkdir weather-app
cd weather-app
npm init -y
npm install axios

Step 2: Create app.js

const axios = require('axios');

async function getWeather(city) {
    try {
        const response = await axios.get(
            `https://wttr.in/${city}?format=%t`
        );
        console.log(`The weather in ${city} is ${response.data}`);
    } catch (error) {
        console.error("Error fetching weather data:", error.message);
    }
}

getWeather("Delhi");

Step 3: Run the app

node app.js

Output example:

The weather in Delhi is +32°C

🎉 You’ve built a mini weather app using NPM and external packages!


NPM vs Yarn vs PNPM

While NPM is the default, there are alternatives like:

  • Yarn → faster installs, better caching.
  • PNPM → saves disk space by reusing packages across projects.

👉 But as a beginner, stick with NPM first since it comes by default.


Best Practices with NPM

  • Always commit your package.json and package-lock.json to version control.
  • Avoid committing the node_modules folder.
  • Use npm audit to check for security vulnerabilities.
  • Prefer exact versions in production to avoid unexpected issues.

Conclusion

In this lesson of our Node.js Tutorial, you learned:

  • What NPM is and why it’s important
  • Initializing a project with npm init
  • Installing, updating, and removing packages
  • Running NPM scripts
  • Building a small Weather App using Axios

With NPM, you now have access to millions of packages that can speed up your development.

👉 Up Next: Day 7 – Node.js Global Objects

Leave a Reply

Your email address will not be published. Required fields are marked *