DEV Community

Cover image for Setting Up Gulp On Node.js
oyedeletemitope
oyedeletemitope

Posted on

Setting Up Gulp On Node.js

Gulp is a cross-platform, streaming task runner that lets developers automate many development tasks. At a high level, gulp reads files as streams and pipes the streams to different tasks. The set tasks are code-based and use plugins.
The tasks modify the files, building source files into production files.
Gulp provides a tool for build task execution in Node.js environments that’s comparable to Gradle in Java environments.
The Node.js API makes it easy to write Gulp pipelines that generate proxy modules.
In this article, I’ll be writing on setting and creating a gulp file on js.

Getting started

If you’ve previously installed gulp globally, run npm rm --global gulp before following these instructions. For more information, read this

Check for node

To verify that you have node.js installed, run

node --version
Enter fullscreen mode Exit fullscreen mode

If it's not, follow the instructions here

Install the gulp command-line utility

You can choose to install gulp globally on your PC by running:

npm install --global gulp-cli
Enter fullscreen mode Exit fullscreen mode

The above command will install gulp-cli globally and you will be able to run the gulp command anywhere, directly from your CLI/terminal.


To install gulp as a one-time dev-dependency in your project:

Create a project directory and navigate into it.

Let's go ahead and create a new project. The mkdir command creates a new folder: my-project, and the cd command navigates to this directory.

mkdir my-project
cd my-project
Enter fullscreen mode Exit fullscreen mode

Create a package.json

To get started, we will need to generate a new package.json which will contain basic information about our project, to do this run:

npm init
Enter fullscreen mode Exit fullscreen mode

This will guide you through giving your project a name, version, description, etc.

Install the gulp package in your devDependencies

npm install --save-dev gulp
Enter fullscreen mode Exit fullscreen mode

Verify your gulp versions

gulp --version
Enter fullscreen mode Exit fullscreen mode

Create a gulp file

Using your text editor, create a file named gulpfile.js in your project root with these contents.

function defaultTask(cb){
// place code for default task here
 cb();
}
exports.default= defaultTask
Enter fullscreen mode Exit fullscreen mode

Test it

From your project directory, run the following command:

gulp
Enter fullscreen mode Exit fullscreen mode

This command will automatically locate your gulpfile.js file and execcute it as a gulp task.

You can also run multiple task with gulp simultaneously by running:

gulp <path/to/task1> <path/to/task2>
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article, we've been able to cover the basics of gulp and how to install it both globally, and also as a one-time devDependency.


Kindly share if you found this helpful.

Top comments (0)