Download a GitHub Repository using Node.JS

Published: | Updated: | by Julian Knight Reading time ~2 min.
📖 Kb | 📎 Development | 🔖 node.js, nodejs, JavaScript, ECMAscript

Downloading a repository from GitHub using Node.JS should be really easy. But it isn’t! This KB post reminds us of the tricks required.

We have to be careful with the source URL names when working with GitHub as the URL’s generally exposed on the web interface are not the actual URL’s. The Node.JS http/https get() functions don’t handle redirections well.

#!/usr/bin/env node
'use strict'

 * Install the Node-RED alternate installation template
 * Author: Julian Knight (Totally Information), Jan. 2019
 * License: MIT

const fs = require('fs')
const https = require('https')

function download(url, dest, cb) {
    const file = fs.createWriteStream(dest);
    const request = https.get(url, function (response) {
        file.on('finish', function () {
            file.close(cb);  // close() is async, call cb after close completes.
    }).on('error', function (err) { // Handle errors
        fs.unlink(dest); // Delete the file async. (But we don't check the result)
        if (cb) cb(err.message);

// Download latest archive from GitHub to temp folder
const dest  = './'
const url = ''
download(url, dest, function(){

Obviously, it would be better to add proper error traps and messaging.

GitHub URL’s 🔗︎

As mentioned, most of the exposed GitHub URL’s actually redirect which upsets Node’s built-in get functions. The downside of using the direct URL’s is that they may change format from time-to-time. If you can, use a 3rd-party library such as request which handles http vs https and redirects without additional programming.

The format for the actual repository archives (in zipped form) is:<userOrOrgName>/<repoName>/zip/<branch>

Where: <branch> is either ‘master’ or a git tag ID (e.g. ‘v1.2.3’) or other git branch ID.

For an individual file from the repository, use:<userOrOrgName>/<repository>/<branch>/<filename>

References 🔗︎

comments powered by Disqus