Gulp: Unable to find gulp task default when requiring a module that defines default gulp task

Created on 20 May 2015  路  5Comments  路  Source: gulpjs/gulp

I am trying to require a gulpfile (someGulpfile.js) in my own gulpfile (gulpfile.js) from where I run the gulp command. When I try to use the tasks defined in someGulpfile it throws an error on console saying Task 'default' is not in your gulpfile even though I just did require('./someGulpfile'); in my gulpfile.js

I found out that this somewhat works but it does not output every task that is run (for example it does not output, Starting sometask, Finished sometask, Starting default, ...) and if there is an error it does not show the error and where it happened but errors out in the gulp library code where it has trouble formatting the error output.

//gulpfile.js
var gulp = require('gulp');
gulp.tasks = require('./someGulpfile').tasks;

//someGulpfile.js
var gulp = require('gulp');
gulp.task('sometask', function() {});
gulp.task('default', ['sometask']);
module.exports = gulp;

I am using version 3.8.11

Most helpful comment

@ankurp It sounds like your package and the other package are using different instances. I ran into a similar situation recently. Maybe there's a better solution, but what I did was like this:

// gulpfile.js
var gulp = require('gulp');
require('path/to/someGulpfileGuts')(gulp);

// someGulpfile.js
require('./someGulpfileGuts.js')();

//someGulpfileGuts.js
module.exports = function (gulp) {
  gulp = gulp || require('gulp');
  gulp.task('sometask', function() {});
  gulp.task('default', ['sometask']);
};

All 5 comments

Remove line #2, it isn't needed, modules are singletons in node

Also check out gulp-hub

@contra I ran into this issue where I have a node_module in my package.json that defines some gulp tasks and it has gulp as its dependency. I then add gulp as a dependency in my own package and for some reason that gulp is not able to find the tasks that I require from the node_module that defines them.

@ankurp It sounds like your package and the other package are using different instances. I ran into a similar situation recently. Maybe there's a better solution, but what I did was like this:

// gulpfile.js
var gulp = require('gulp');
require('path/to/someGulpfileGuts')(gulp);

// someGulpfile.js
require('./someGulpfileGuts.js')();

//someGulpfileGuts.js
module.exports = function (gulp) {
  gulp = gulp || require('gulp');
  gulp.task('sometask', function() {});
  gulp.task('default', ['sometask']);
};

Hi. This work in my case.
I was facing the "gulp task is not in your gulpfile" error message, when using "gulp-require-tasks"
passing the gulp Instance solve the problem :

var gulp            = require('gulp');
var requireTasks    = require('gulp-require-tasks');

requireTasks({
    passGulp: true,
    gulp: gulp
}); 

Many thanks for your post !

Was this page helpful?
0 / 5 - 0 ratings