How to get Trending GitHub Repositories Using Node.js ?


  • Fetch the entire HTML page and store it as string using request package.
  • Load HTML into cheerio and find the CSS selectors to extract repositories details.

Using request package:
request package: The request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.

Installing request package:

$ npm install request

Note: Run this command in current folder, where ‘app.js’ file located.

Syntax for request:

request(url, (error, response, body) => {
    if(!error && response.statusCode == 200) {
        statements to be executed.


  • url: API endpoint to which request is made.
  • response: HTTP response status codes indicate whether a specific HTTP request has been successfully completed.
  • body: Response data.

Using cheerio package:
cheerio package: Fast, flexible & lean implementation of core jQuery designed specifically for the server.

Installing cheerio package:

$ npm install cheerio

Note: Run this command in current folder, where ‘app.js’ file located.

Syntax for cheerio:

const cheerio = require('cheerio')
const $ = cheerio.load(HTMLString)
$(CSS Selector).text('Hello there!')




// Import request package
const request = require('request');
// Import cheerio package
const cheerio = require('cheerio');
// Github Trending Page URL
// Get request to the URL
request.get(url, (error, response, body) => {
    // If the response code is 200 and
    // there is no error
    if (!error && response.statusCode == 200) {
        // Load HTML string into cheerio
        const $ = cheerio.load(body);
        // Below are the CSS selectors to 
        // fetch the data required
        let temp = $('.Box-row')
        let repos = $('.h3.lh-condensed a');
        let data = [];
        for (let i = 0; i < repos.length; i++) {
            let reponame = $(temp[i])
                .find('.h3.lh-condensed a')
                /[\n\r]+|[\s]{2, }/g, ' ').trim();
            let repolanguage = $(temp[i])
' span span[itemprop="programmingLanguage"]')
                .replace(/[\n\r]+|[\s]{2, }/g, ' ').trim();
            let repostars = $(temp[i])
                .replace(/[\n\r]+|[\s]{2, }/g, ' ').trim();
            // Push the fetched data into an object
                'Repository': reponame,
                'Language': repolanguage,
                'Stars/Forks': repostars
        // Display the Object created using console.table
    else {
        console.log("Unable to fetch data from github");



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.