Tracking npm Package Monthly Downloads with Node.js
In this blog post, I 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.
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, I 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.
Prerequisites: Before getting started, ensure that you have Node.js installed on your machine. You'll also need a basic understanding of JavaScript and NPM.
Setting up the Project
- Create a new directory for your project and navigate to it using the command line.
- Initialize a new Node.js project by running the command:
npm init -y
- Install the
node-fetch
package, which allows us to make HTTP requests, by running:npm install node-fetch
Creating the Script
- Create a new file called
index.mjs
in your project directory. - Open
index.mjs
in a text editor and copy the following code:
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
- Open your command line and navigate to the project directory.
- Run the command:
npm start
ornode start
to execute thestart
script specified in thepackage.json
file. - 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.
Happy tracking and happy coding!