Gulp getting started
last modified November 15, 2026
This article is an introduction to Gulp, a task runner for Node.js. It covers
Gulp 5 with gulpfiles written as ES modules. We show how to define tasks, how
Gulp knows that a task has finished, how to select files with globs, and how to
compose tasks with series and parallel.
Gulp
Gulp is a streaming build system for front-end and Node.js projects. It automates repetitive work such as copying and renaming files, compiling Sass, minifying JavaScript and CSS, optimising images, or rebuilding the project whenever a source file changes.
Gulp favours code over configuration. The tasks are plain JavaScript functions
defined in a file called the gulpfile. Most of the actual work is
done by plugins, which are small, single-purpose packages such as
gulp-rename or gulp-terser.
Gulp is built on Node streams. The src function reads files from
the file system and turns them into a stream of virtual file objects. The
pipe method passes the files through a chain of plugins, each of
which transforms them in memory. The dest function finally writes
the transformed files to an output directory. Because the intermediate results
stay in memory, no temporary files are needed between the steps.
Gulp 5 was released in 2024. It requires Node.js 10.13 or later, uses the
streamx library for its streams, reads files as UTF-8 text by
default (binary files such as images need the encoding: false
option of src), and loads gulpfile.mjs files as ES
modules.
Gulp CLI
Gulp consists of two parts: the gulp library, which we import in
the gulpfile, and the command line tool, which finds the gulpfile and runs the
requested tasks. The gulp package ships with the command line
tool, so after installing it locally in a project we can run it with
npx gulp.
Alternatively, we can install the gulp-cli package globally with
npm i --global gulp-cli; then the gulp command is
available directly in the shell and always delegates to the Gulp version
installed in the current project. In this article we use npx gulp.
Installing Gulp
We create a Node.js project and install Gulp as a development dependency.
We use Node.js 22. The --version option shows the version of the
command line tool and of the Gulp library installed in the project.
$ node -v v22.19.0 $ mkdir gulp-intro $ cd gulp-intro $ npm init -y $ npm i -D gulp $ npx gulp --version CLI version: 3.1.0 Local version: 5.0.1
Gulp tasks
A Gulp task is a function. Every function exported from the gulpfile is a
public task, which can be run from the command line. Functions that
are not exported are private tasks; they can only be used inside the
gulpfile, typically as parts of a composed task. The default export becomes the
default task, which runs when we call gulp without a
task name.
Gulp tasks are asynchronous. A task must tell Gulp when it is done: it can
call the callback function it receives as its first argument, or return a
stream, a promise, an event emitter, a child process, or an observable. An
async function returns a promise, so it signals completion
automatically.
export function hello(cb) {
console.log('Hello from Gulp');
cb();
}
export async function today() {
const now = new Date();
console.log(`Today is ${now.toDateString()}`);
}
The gulpfile defines two public tasks. Because the file has the
.mjs extension, Node loads it as an ES module and we can use
export and import regardless of the
type field in package.json.
export function hello(cb) {
console.log('Hello from Gulp');
cb();
}
The hello task signals completion by calling the
cb callback.
export async function today() {
const now = new Date();
console.log(`Today is ${now.toDateString()}`);
}
The today task is an async function; Gulp waits until
the returned promise is settled.
We run both tasks by name and list the public tasks with the
--tasks option. Running gulp without a task name
fails, because the gulpfile has no default export.
$ npx gulp hello [12:16:25] Using gulpfile ~/Documents/prog/js/gulp-intro/gulpfile.mjs [12:16:25] Starting 'hello'... Hello from Gulp [12:16:25] Finished 'hello' after 1.22 ms $ npx gulp today [12:16:25] Using gulpfile ~/Documents/prog/js/gulp-intro/gulpfile.mjs [12:16:25] Starting 'today'... Today is Sun Nov 15 2026 [12:16:25] Finished 'today' after 1.33 ms $ npx gulp --tasks Tasks for ~/Documents/prog/js/gulp-intro/gulpfile.mjs ├── hello └── today $ npx gulp [12:16:26] Using gulpfile ~/Documents/prog/js/gulp-intro/gulpfile.mjs [12:16:26] Task never defined: default To list available tasks, try running: gulp --tasks
Forgetting to signal completion
A task that neither calls the callback nor returns anything leaves Gulp waiting. When the Node process has nothing else to do, Gulp reports the task as not completed.
export function broken() {
console.log('This task never signals completion');
}
The broken task is a synchronous function that ignores its
callback. This is the most common mistake in new gulpfiles; the fix is to
return the stream or promise the task works with, call the callback, or make
the function async.
When we run it, the task prints its message, but Gulp marks it as unfinished and exits with a non-zero status.
$ npx gulp broken [12:16:30] Using gulpfile ~/Documents/prog/js/gulp-intro/gulpfile.mjs [12:16:30] Starting 'broken'... This task never signals completion [12:16:30] The following tasks did not complete: broken Did you forget to signal async completion?
Globbing
The src function selects files with globs. A glob is a
string of literal and wildcard characters, such as *,
**, or !, matched against file paths. The
src function accepts a single glob or an array of globs.
The first glob matches all files ending in .js in the base directory,
but not in its subdirectories.
*.js
The ** part matches any number of nested directories, so the next
glob matches all JavaScript files in the base directory and all its
subdirectories.
**/*.js
A glob starting with ! excludes files. The following array matches all
SCSS files except partials, whose names start with an underscore. A negative
glob must follow at least one positive glob in the array.
['*.scss', '!_*.scss']
Brace expansion matches several alternatives; the last glob selects files that end
either in .scss or in .sass.
*.{scss,sass}
Renaming files
In the first real example, we use the gulp-rename plugin to change
the extension of HTML files from .htm to .html.
We install the plugin. The src directory contains three
.htm files.
$ npm i -D gulp-rename $ ls src/ about.htm contact.htm index.htm js styles
import { src, dest } from 'gulp';
import rename from 'gulp-rename';
export function renameFiles() {
return src('src/*.htm')
.pipe(rename({ extname: '.html' }))
.pipe(dest('dist/'));
}
The renameFiles task reads the .htm files, renames
them, and writes them to the dist directory. The original files
stay untouched.
return src('src/*.htm')
.pipe(rename({ extname: '.html' }))
.pipe(dest('dist/'));
The src function creates a stream of the files matched by the glob.
The pipe method passes every file to the rename
plugin, which replaces the extension; dest writes the files to
dist, creating the directory if necessary. The task returns the
stream, which is how it signals completion.
We run the task and list the output directory, which holds the renamed copies.
$ npx gulp renameFiles [12:16:30] Using gulpfile ~/Documents/prog/js/gulp-intro/gulpfile.mjs [12:16:30] Starting 'renameFiles'... [12:16:30] Finished 'renameFiles' after 15 ms $ ls dist/ about.html contact.html index.html
Minifying JavaScript
The gulp-terser plugin minifies JavaScript with the Terser
library. It removes whitespace and comments, shortens local variable names,
and simplifies expressions.
We install the plugin.
$ npm i -D gulp-terser
function greet(name) {
const message = `Hello ${name}!`;
return message;
}
console.log(greet('there'));
This is a small script with one function.
import { src, dest } from 'gulp';
import terser from 'gulp-terser';
export default function () {
return src('src/js/main.js')
.pipe(terser())
.pipe(dest('dist/js'));
}
The default export becomes the default task. It passes the script
through Terser and writes the result to dist/js.
We run the default task without a task name and print the result. Terser renames the parameter and drops the temporary variable.
$ npx gulp
[12:16:37] Using gulpfile ~/Documents/prog/js/gulp-intro/gulpfile.mjs
[12:16:37] Starting 'default'...
[12:16:37] Finished 'default' after 30 ms
$ cat dist/js/main.js
function greet(e){return`Hello ${e}!`}console.log(greet("there"));
Composing tasks
The series function combines tasks to run one after another, and
parallel combines tasks to run at the same time. Both return a new
task, and they can be nested.
In the following composition, tasks a and b run
concurrently; when both are
complete, c runs, and after it finishes, d runs.
series(parallel(a, b), series(c, d))
The next example builds a small project. It minifies CSS with
gulp-clean-css and JavaScript with gulp-terser, adds
a .min suffix with gulp-rename, and deletes the old
output first with the del package.
We install the two remaining packages. Version 9 of del is an ES
module that exports the deleteAsync function.
$ npm i -D gulp-clean-css del
The project has a script and a stylesheet in the src directory.
The main.js file is the one from the previous section.
src/
├── js
│ └── main.js
└── styles
└── main.css
/* page layout */
body {
font-family: Georgia, serif;
font-size: 1em;
line-height: 1.7em;
background: #333333;
text-align: center;
}
h1 {
margin: 0px 0px 10px 0px;
color: #ffffff;
}
This is a simple stylesheet with a comment and long colour values that the minifier can shorten.
import { src, dest, series, parallel } from 'gulp';
import terser from 'gulp-terser';
import rename from 'gulp-rename';
import cleanCSS from 'gulp-clean-css';
import { deleteAsync } from 'del';
export function clean() {
return deleteAsync(['dist']);
}
export function styles() {
return src('src/styles/*.css')
.pipe(cleanCSS())
.pipe(rename({ suffix: '.min' }))
.pipe(dest('dist/styles'));
}
export function scripts() {
return src('src/js/*.js')
.pipe(terser())
.pipe(rename({ suffix: '.min' }))
.pipe(dest('dist/js'));
}
export const build = series(clean, parallel(styles, scripts));
export default build;
The gulpfile has three basic tasks and one composed task, which is also the default task.
export function clean() {
return deleteAsync(['dist']);
}
The clean task removes the dist directory. The
deleteAsync function returns a promise, which the task returns to
signal completion.
export function styles() {
return src('src/styles/*.css')
.pipe(cleanCSS())
.pipe(rename({ suffix: '.min' }))
.pipe(dest('dist/styles'));
}
The styles task minifies the stylesheets and inserts
.min before the extension, so main.css becomes
main.min.css.
export const build = series(clean, parallel(styles, scripts)); export default build;
The build task first runs clean; after it finishes,
styles and scripts run in parallel. The same composed
task is exported as the default task.
The --tasks option shows the structure of the composed tasks.
$ npx gulp --tasks Tasks for ~/Documents/prog/js/gulp-intro/gulpfile.mjs ├─┬ build │ └─┬ <series> │ ├── clean │ └─┬ <parallel> │ ├── styles │ └── scripts ├── clean ├─┬ default │ └─┬ <series> │ ├── clean │ └─┬ <parallel> │ ├── styles │ └── scripts ├── scripts └── styles
We run the build task and look at the result. Both
styles and scripts start before either of them
finishes, because they run in parallel. The minified stylesheet has no comment
or whitespace, and the colours are shortened.
$ npx gulp build
[12:16:37] Using gulpfile ~/Documents/prog/js/gulp-intro/gulpfile.mjs
[12:16:37] Starting 'build'...
[12:16:37] Starting 'clean'...
[12:16:37] Finished 'clean' after 8.58 ms
[12:16:37] Starting 'styles'...
[12:16:37] Starting 'scripts'...
[12:16:37] Finished 'styles' after 34 ms
[12:16:37] Finished 'scripts' after 35 ms
[12:16:37] Finished 'build' after 45 ms
$ tree dist
dist
├── js
│ └── main.min.js
└── styles
└── main.min.css
2 directories, 2 files
$ cat dist/styles/main.min.css
body{font-family:Georgia,serif;font-size:1em;line-height:1.7em;background:#333;text-align:center}h1{margin:0 0 10px 0;color:#fff}
Source
Gulp quick start, Gulp async completion, Explaining globs, Gulp series API
In this article we have introduced Gulp 5. We have written tasks in an ES
module gulpfile, signalled their completion, selected files with globs, and
composed tasks with series and parallel. See also the
Gulp Sass and Gulp Terser
tutorials.
Author
List all Gulp tutorials.