> ## Content Index
> Fetch the complete content index at: https://www.boldoutlook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Tracking npm Package Monthly Downloads with Node.js
- URL: https://www.boldoutlook.com/tracking-npm-package-monthly-downloads-nodejs/
- Published: 2023-07-03T13:00:35.000Z
- Updated: 2025-07-26T02:46:23.000Z
- Description: In this blog post, we will share how to create a Node.js script that fetches and displays the monthly download counts of multiple NPM packages using the npm registry API.
- Author: Brandon Himpfen
- Tags: Digital, Nodejs, JavaScript, Programming, NPM, #Import 2026-10-18 13:20

As a developer, it's important to keep track of the popularity and usage of your NPM packages. Monitoring the download counts of your packages will help you understand their reach and impact within the development community. In this blog post, we will share how to create a [Node.js](https://www.boldoutlook.com/tag/nodejs/) script that fetches and displays the monthly download counts of multiple NPM packages using the npm registry API.

**Prerequisites**: Before getting started, ensure that you have Node.js installed on your machine. You'll also need a basic understanding of [JavaScript](https://www.boldoutlook.com/tag/javascript/) and [NPM](https://www.boldoutlook.com/tag/npm/).

## Setting up the Project

1. Create a new directory for your project and navigate to it using the command line.
2. Initialize a new Node.js project by running the command: `npm init -y`
3. Install the `node-fetch` package, which allows us to make HTTP requests, by running: `npm install node-fetch`

## Creating the Script

1. Create a new file called `index.mjs` in your project directory.
2. Open `index.mjs` in a text editor and copy the following code:

```javascript
import fetch from 'node-fetch';

async function fetchDownloadCounts(packages) {
  const baseUrl = 'https://api.npmjs.org/downloads/point/last-month';
  const downloadCounts = {};

  let totalDownloads = 0;

  for (const packageName of packages) {
    const url = `${baseUrl}/${packageName}`;
    try {
      const response = await fetch(url);
      const data = await response.json();
      const packageDownloads = data.downloads;
      downloadCounts[packageName] = packageDownloads;
      totalDownloads += packageDownloads;
    } catch (error) {
      console.error(`Failed to fetch download counts for ${packageName}:`, error);
    }
  }

  downloadCounts['Total Downloads'] = totalDownloads;

  return downloadCounts;
}

// Example usage
const packages = [
  'generator-html-template',
  '@brandonhimpfen/loremipsum-generator',
  '@brandonhimpfen/markdown-to-json',
  'social-media-campaign-roi-calculator',
  'generator-ratchet-boilerplate',
  'generator-wordpress-htaccess',
  'generator-awesome-lists',
  'bh-basecss',
  'basecss-stylus',
  'basecss-sass',
  'ads.css',
  'css-media-queries-boilerplate',
  'social-sharing-buttons',
  'bh-buttons',
  'social-sharing-buttons-sass',
  'seo-sitemap-generator',
  'seo-content-optimizer',
  '@brandonhimpfen/is-ip-address',
  'social-media-math',
  'gradient-css-generator',
  'seo-friendly-urls',
  'is-stop-words',
  'colorful-cli',
  'is-hex-color-code',
  'html-list-builder'
];

fetchDownloadCounts(packages)
  .then((downloadCounts) => {
    console.log('Download Counts:', downloadCounts);
  })
  .catch((error) => {
    console.error('Error:', error);
  });

```

Replace the `packages` array with the names of the NPM packages you want to track.

## Running the Script

1. Open your command line and navigate to the project directory.
2. Run the command: `npm start` or `node start` to execute the `start` script specified in the `package.json` file.
3. After running the previous command, execute the command: `node --experimental-modules index.mjs`

By following this guide, you will learn how-to create a Node.js script to fetch and display monthly download counts for multiple NPM packages. Remember to run the `npm start` or `node start` command before executing the `node --experimental-modules index.mjs` command. You can now monitor the popularity and usage of your packages, as well as calculate the total combined downloads. Feel free to customize the script further according to your needs, such as tracking different time periods or incorporating the functionality into a larger application.