As web developers, we sometimes find ourselves repeating the same tedious tasks again and again. If you consider how much time is wasted by running a build command or hitting refresh on your browser, you will realize that you can be saving a lot of time. Additionally, by automating your processes, you can stay focused on the task at hand instead of temporarily leaving “the zone” (your state of productivity).

In this JavaScript automation tutorial, you will learn how to automate your design and development process with Gulp. If you are more design-oriented, I encourage you to overcome any fears you have and to read on. On the other hand, if you are a developer you will be able to sweep right through this once you understand the logic behind it.

javascript automation with Gulp

Gulp is a build system that employs Node.js’s streams to implement an asynchronous source-destination approach to automation. Everything is written in JavaScript, so it is easy for anyone with intermediate coding knowledge to get started. A Gulp build process consists of a collection of watchers and tasks. Additionally, the community behind Gulp maintains a huge plugin directory within npm which helps accomplish any task from concatenating JavaScript to creating icon fonts from SVGs.

Alternatives to Gulp

There are plenty of alternatives out there, most of which have spawned in the past couple of years - the most notable one being Grunt. The contest between Gulp and Grunt will never have a clear winner, since they both have their pros and cons, hence I will avoid delving deep into that. In a nutshell, Grunt’s heavy reliance on config is what steers people towards Gulp’s JavaScript approach. In the meantime, native GUI implementations such as Koala have gained some ground, mostly from people that withhold getting into coding. However, with the bundled applications it’s impossible to reach the level of customizability and extendability that Gulp offers.

Process Automation Fundamentals


Plugins are the means through which gulp accomplishes each process. Plugins are installed through npm and initiated using “require”.


For Gulp, tasks always have a source and a destination. In between them lie the pipes that call each plugin and output the transmuted results to the next pipe.


Globs are wildcard patterns that refer to files. Globs or arrays of globs are used as inputs in task source.


The watchers watch the source files for changes and call tasks whenever a change is detected.


This is the JavaScript file that the “gulp” command points at. It contains everything from the tasks to the watchers or other pieces of code used by tasks.

A Straightforward Task

To get started, you need Node.js and a command line shell with administrator access. You can download Node.js from here. Once installed, you can run the following command to ensure that npm is up to date.

sudo npm install npm -g

The -g flag states that the installation will be global.

Now you are ready to install Gulp and start calling some simple tasks. Use the following command to install Gulp globally.

sudo npm install gulp -g

Note that you can also use the same command to update.

You can download the starter kit that will help you with your first task from toptal-gulp-tutorial/step1. It includes a very simple task that compiles SCSS files to CSS. It is using the gulp-sass plugin, which employs libsass. I have chosen libsass here because it is way faster than the Ruby alternative, although it lacks some of its functionality. Once in the root directory of your project, you can use the following command to install all the plugins required.

npm install

This command reads the package.json file and installs all the dependencies required. We are using “npm install” here as a shorthand for the following:

npm install gulp --save-dev
npm install gulp-sass --save-dev

The “–save-dev” flag adds the selected plugins to the package.json devDependencies so that the next time you want to install everything, you can use the handy “npm install”.

At this point you can run your first task. Run the following command and watch as all the SCSS files in the /scss folder are compiled into CSS in the corresponding directory:

gulp scss

Note that in this example we are specifying the task to be run. If we omitted the task name, the one named “default” would be run.

Code Walkthrough

The above is accomplished in only 7 lines of JavaScript code. Surely, you will feel at home once you grasp the simplicity of it:

var gulp = require('gulp');
var scss = require('gulp-sass');

In the beginning we initialize all the plugins that we will be using. Gulp is the only one we can’t do without:

gulp.task('scss', function() {

We define the task named “scss”:

return gulp.src('scss/*.scss')

Set the source files for the task. These are defined as globs. In this example we are referring to all the files in the “scss/” folder that end in “.scss”.


At this point, we call the gulp-sass plugin which we previously defined:


Finally, we use “gulp.dest” to define the destination folder for our files. This could also be a single file name if the files where concatenated.

gulp process automation implementation

To further improve this process automation implementation, you may try to include a few other Gulp plugins. For instance, you may want to minify the final product of your task using gulp-minify-css and automatically add vendor prefixes with gulp-autoprefixer.

Employing a Watcher

I have created a watcher starter kit that you can use straight away. You can download it from toptal-gulp-tutorial/step2. It includes an enhanced version of the previously created SCSS task, and a watcher that watches the source files and calls the task. In order to start it, use the following command:


This command starts the “default” task which initiates the watcher. At this point you can edit any of the SCSS files and watch as the CSS files get recompiled. You will be able to see Gulp’s notifications in the command line.

Code Walkthrough

We have set up a watcher for our task with only 3 extra lines of code. That said, the watcher starter kit does not differ much from the introductory example. In this section, we will go through all the additions and alterations.

return gulp.src(['scss/**/*.scss', '!scss/**/_*'])

In this example, Gulp source is provided with an array of globs. The first one includes all the files that end in “.scss” also in subfolders, and the second one excludes the ones that start with “_”. This way we can use SCSS’s built-in function @import to concatenate the _page.scss file.

gulp.task('default', ['scss'], function() {

Here we define the “default” task. The “scss” task is run as a dependency before “default”.'scss/**/*.scss', ['scss']);

Finally, we call Gulp’s watch function to point at any file ending with “.scss” and whenever a change event occurs, to run the “scss” task.

Now you are ready to create new watchers for other automated processes such as JavaScript concatenation, code hinting, CoffeeScript compilation, or anything that else that might come in mind. To delve deeper into this JavaScript automation implementation, I propose adding gulp-notify which will notify you when a task is run. Also, you can create a separate task to minify the resulting CSS code and make the “scss” task run as a dependency to that. Finally, you can use gulp-rename to add the “.min” suffix to the resulting files.

Advanced Gulp Plugins for JavaScript Automation

There exist thousands of plugins in Gulp’s plugin base, some of which go far beyond simple JavaScript automation of a build process. Here are a few outstanding examples:


BrowserSync injects CSS, JavaScript and automatically refreshes the browser whenever a change is made. Additionally, it contains a ghostMode feature which can be used to scare your colleagues or to greatly speed up your browser testing.


Browserify analyzes the “require” calls in your application and converts it to a bundled JavaScript file. Also, there is a list of npm packages that can be converted into browser code.


Similar to Browserify, Webpack aims to convert modules with dependencies to static files. This one gives more freedom to the user as to how the module dependencies will be set, and does not aim to follow Node.js’s code style.


Gulp-karma brings the infamous testing environment to Gulp. Karma follows the least configuration approach that Gulp also endorses.


gulp and javascript automation

In this process automation tutorial I have demonstrated the beauty and simplicity of using Gulp as a build tool. By following the steps described in the tutorial you will now be ready to fully automate your software development process both in your future and your legacy projects. Investing some time into setting up a build system for your older projects will certainly save you precious time in the future.

Stay tuned for a more advanced Gulp tutorial coming soon.

About the author

Antonios Minas, Greece
member since December 5, 2014
Antonios is a senior software engineer specializing in front-end, WordPress, Blockchain technology, PHP, and JavaScript frameworks with more than six years of professional experience spanning from the IT sector to digital advertising. He is passionate about applying top-end solutions and software engineering patterns in order to solve complex problems. [click to continue...]
Hiring? Meet the Top 10 Freelance Node.js Developers for Hire in June 2017


Evan Wieland
Hey, great post Antonios! I've been wanting to switch over from grunt to try out gulp for a while now, but just haven't had time to even go over the basic differences between gulp and grunt. I like how clean gulp is. Thanks for this insight! @Luboš, kudos on the art as well. Looks great!
I strongly encourage you not to do package management with sudo. It's better to run once command like this: sudo chown -R $USER /usr/local/bin and then you'll have ability to install most of npm modules globally without sudo. Synchronization of streams in gulp isn't good enough yet. But it's a very good for anything else related to building js/coffee applications :) Thank you, Antonios, for a pretty deep tour to gulp!
Shahzad Soomro
Nice Tutorial for gulp Introduction !
Filip Sotirov
Hey, check out the gulp-ruby-sass ;)
Gavin Engel
I agree, using npm install -g is something to be avoided. In cases where you aren't allowed to sudo at all, you can use this method for ~/bin/_npm:
Gavin Engel
Just to be a pain in the butt ... there are some people that see gulp/grunt as superfluous. Since bash/zsh commands are available to Windows users with git shell/cygwin, you can definitely avoid gulp/grunt for build scripts and just use npm run scripts.
Filip Dupanović
Read the satire on ./task.js before you rofltumble yourself into a task runner
Dimitar Ivanov
Nice tutorial, thanks. In addition, to watch for changes use the gulp-watch plugin. See how:
Baaddin Baaddin
Hi, thanks for this great article. i want to ask this when we use gulp in dev environment like use in local and when we need make a change in js or sass what could we do this ? should we get on local all files from server or whatever ? i did it as first but i think it is bad way to do that. please tell me how we do that ? if you give an example i would be grateful
comments powered by Disqus
The #1 Blog for Engineers
Get the latest content first.
No spam. Just great engineering posts.
The #1 Blog for Engineers
Get the latest content first.
Thank you for subscribing!
You can edit your subscription preferences here.
Trending articles
Relevant Technologies
About the author
Antonios Minas
Front End Developer
Antonios is a senior software engineer specializing in front-end, WordPress, Blockchain technology, PHP, and JavaScript frameworks with more than six years of professional experience spanning from the IT sector to digital advertising. He is passionate about applying top-end solutions and software engineering patterns in order to solve complex problems.