Is it possible to have one main gulpfile.js from which to call tasks from other gulp files.js? Simple "require" of child gulpfile.js into main one doesn't work. I have a platform project which includes several sub projects with separate gulpfiles, so I need a solution to manage all child gulpfiles from within main one
-
Using `require` to import other gulpfiles should work. What errors are you seeing? – Ben Aug 27 '14 at 19:54
-
You're right it works but in form of: `var childGulp = require('./../path')();` Then I should assign `parentGulp.tasks = childGulp.tasks;`. Now console.log(parentGulp), shows childGulp's tasks. – netanalyzer Aug 28 '14 at 05:16
-
Were you able to fix this. I am also running into same issue and have to do `var gulp = require('gulp');` and then `gulp.tasks = require('./path')().tasks;` – Encore PTL May 20 '15 at 18:06
-
If you have a moment, please consider changing the selected answer to [Jonatan's answer](https://stackoverflow.com/a/67115709/1992129). I commented on his answer highlighting gulp supports and encourages splitting (with a link to gulp) -- even though gulps documentation lacked, I was able to follow Jonatan's answer to import tasks into a central gulp file. – Arthur Weborg Dec 06 '21 at 17:01
4 Answers
It is possible to have one main gulpfile.js from which to call tasks from other gulp files.js using the require-dir
module. From the projects README use it like this:
Split tasks across multiple files
If your gulpfile.js
is starting to grow too large, you can split the tasks
into separate files by using the require-dir
module.
Imagine the following file structure:
gulpfile.js
tasks/
├── dev.js
├── release.js
└── test.js
Install the require-dir
module:
npm install --save-dev require-dir
Add the following lines to your gulpfile.js
file.
var requireDir = require('require-dir');
var dir = requireDir('./tasks');

- 4,609
- 1
- 24
- 44
-
1And what if I want to make it future-proof and don't want to install another package for it. So for example I just want to use taskList = require('fs').readdirSync(taskPath), then creating a single function to get every task files with gulp, plugins (which for I use gulp-load-plugins), config dependencies. How can I call one task in a task file from another task file? Or how can I include it? – CreativeZoller Jun 09 '16 at 12:01
-
I've create a special gulp-require-tasks module that will help you to split your gulpfile.js
into separate smaller task files. Please see the README for example and documentation.
Please consider using it and let me know if it works for you! If you have any suggestions or ideas for improvement, I will gladly accept them.

- 26,865
- 29
- 124
- 202
And what if I want to make it future-proof and don't want to install another package for it.
The following works for me with gulp 4, without any extra plugins.
In taskfile.js
:
const { src, dest } = require('gulp');
const mytask = function () {
return src('assets/**/*')
.pipe(dosomething())
.pipe(dest('dest');
}
module.exports = {
mytask
}
In gulpfile.js
:
const { mytask } = require('taskfile.js');
// use in other tasks
gulp.task('manythings', gulp.series(..., mytask, ...));
// or use directly as 'gulp mytask'
module.exports = {
mytask
}

- 1,445
- 1
- 16
- 24
-
Although [the selected answer](https://stackoverflow.com/a/26532942/1992129) is/was a functional answer, this answer appears to be the correct answer for the current in that [gulp supports and encourages splitting up gulp files natively](https://gulpjs.com/docs/en/getting-started/javascript-and-gulpfiles#splitting-a-gulpfile). – Arthur Weborg Dec 06 '21 at 16:56
-
Adding to that, I had no issues following this logic and incorporating all my gulpfiles/tasks from those files into a single main gulpfile at the root. I like how this required no additional dependencies. – Arthur Weborg Dec 06 '21 at 17:03
I would recommend this answer for anyone who doesn't want to include a separate module or refactoring:
https://stackoverflow.com/a/5809968/40769
var fs = require('fs');
// file is included here:
eval(fs.readFileSync('tools.js')+'');

- 3,538
- 2
- 24
- 24