Need to ZIP an entire directory using Node.js

This is another library which zips the folder in one line : zip-local

var zipper = require('zip-local');"./hello/world/").compress().save("");

I'm not going to show something new, just wanted to summarise the solutions above for those who like Promises as much as I do 😉.

const archiver = require('archiver');

 * @param {String} sourceDir: /some/folder/to/compress
 * @param {String} outPath: /path/to/
 * @returns {Promise}
function zipDirectory(sourceDir, outPath) {
  const archive = archiver('zip', { zlib: { level: 9 }});
  const stream = fs.createWriteStream(outPath);

  return new Promise((resolve, reject) => {
      .directory(sourceDir, false)
      .on('error', err => reject(err))

    stream.on('close', () => resolve());

Hope it will help someone 🤞

Use Node's native child_process api to accomplish this.

No need for third party libs. Two lines of code.

const child_process = require("child_process");
child_process.execSync(`zip -r <DESIRED_NAME_OF_ZIP_FILE_HERE> *`, {

The example above showcases the synchronous API. You can also use child_process.exec(path, options, callback) if you want async behavior. There are a lot more options you can specify other than cwd to further fine-tune your request.

If you don't have the ZIP utility:

This question is specifically asks about the zip utility for archiving/compression purposes. Therefore, this example assumes you have the zip utility installed on your system. For completeness sakes, some operating systems may not have utility installed by default. In that case you have at least three options:

  1. Work with the archiving/compression utility that is native to your platform

    Replace the shell command in the above Node.js code with code from your system. For example, linux distros usually come with tar/gzip utilities:


    This is a nice option as you don't need to install anything new onto your operating system or manage another dependency (kind of the whole point for this answer).

  2. Obtain the zip binary for your OS/distribution.

    For example on Ubuntu: apt install zip.

    The ZIP utility is tried and tested for decades, it's fairly ubiquitous and it's a safe choice. Do a quick google search or go to the creator, Info-ZIP's, website for downloadable binaries.

  3. Use a third party library/module (of which there are plenty on NPM).

    I don't prefer this option. However, if you don't really care to understand the native methods and introducing a new dependency is a non-issue, this is also a valid option.

I ended up using archiver lib. Works great.


var file_system = require('fs');
var archiver = require('archiver');

var output = file_system.createWriteStream('');
var archive = archiver('zip');

output.on('close', function () {
    console.log(archive.pointer() + ' total bytes');
    console.log('archiver has been finalized and the output file descriptor has closed.');

archive.on('error', function(err){
    throw err;


// append files from a sub-directory, putting its contents at the root of archive, false);

// append files from a sub-directory and naming it `new-subdir` within the archive'subdir/', 'new-subdir');