Skip to content
Advertisement

How to automatically zip files with Node.js and npm

Is there a way to automatically zip certain files at the build time with Node.js and npm?

For example, I have a project, that file structure looks like this:

JavaScript

I want to be able to zip lib folder, certain modules from node_modules and index.js into some zip archive to upload it on the AWS Lambda, for example. I do not need test folder or test Node.js modules (mocha and chai) to be zipped. I have even created a bash script for generating zip file, but is there a way to automatically execute this script, when ‘npm install’ is called?

This should be a standard problem and it should have a standard solution, but I was unable to discover such.

UPDATE

thanks to michael, decided to use gulp. This is my script, in case some one else will need it for AWS Lambda:

JavaScript

Advertisement

Answer

I would go with gulp using gulp-sftp, gulp-tar and gulp-gzip and an alias as command. Create a file called .bash_aliases in your users home folder containing

JavaScript

After a reboot you can call both actions at once with this alias.

A gulp file could look something like this

JavaScript

Of course, you can also add gulp-watch to automatically create the tar/zip and upload it whenever there is a change in the directory.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement