NPM Trends - Visualize The Popularity Of JavaScript Packages

NPM (Node Package Manager) is a package manager for JavaScript packages that developers can use in their projects. The NPM registry contains over 1.5 million packages, providing a centralized repository for developers to share and publish their modules. NPM has become the most popular package manager for JavaScript, and it's crucial to know the trends in downloads of packages in the NPM registry.

What are NPM Trends?

NPM trends refer to the usage and popularity of packages in the NPM registry over time. These trends can be used to track the growth and adoption of packages, as well as to identify which packages are the most popular and widely used. There are many different metrics that can be used to analyze NPM trends, such as total downloads, downloads over a given time frame, and the number of dependent packages.

Analyse NPM Trends
 

Getting the data

The first step is to get the data for the NPM package you want to display. You can use the NPM trends API to retrieve this information. The API provides data on the number of downloads of packages over a specified time range. To get this data, you can make an AJAX request to the API using jQuery.

$.ajax({
    url: 'https://api.npmjs.org/downloads/range/last-month/' + packageName,
    type: 'GET',
    success: function(data) {
        var npmTrendData = data;
        // Do something with the data
    }
});

Plotting the data

Once you have the data, you can use CanvasJS charting library to create a chart to visualize the trends. CanvasJS is a JavaScript library for creating charts and graphs, providing a wide range of chart types and customization options. To create a chart with CanvasJS, you need to create an array of datapoints from the NPM trends data, and then pass it to chart options along with other options like chart-title, axis-title, etc.

<div id="chartContainer" style="height: 300px; width: 100%;"></div>
<script type="text/javascript">
var chart = new CanvasJS.Chart("chartContainer", {
    title: {
        text: "NPM Package Download Trends"
    },
    axisY: {
        title: "Downloads"
    },
    data: [{
        type: "line",
        dataPoints: npmTrendData
    }]
});
chart.render();
</script>

It's worth noting that the NPM registry receives a large number of requests, so there may be rate limits in place to prevent excessive use of the API. Be sure to review the NPM API documentation for more information on usage restrictions and best practices.

Check this JSFiddle (https://jsfiddle.net/vishwas_r/5c36mqrh/) for complete working code.