Search code examples
node.jsframeworksgulpshared-librariesshared

Can you share a gulp framework between projects?


I have many projects that all follow specific folder structures and conventions.

Rather than duplicating the whole project, I'd like to just have a configuration object for each project and each project links to the global framework.

Example:

/gulp-framework
    index.js
/project-a
    gulpfile.js
    configuration.js
/project-b
    gulpfile.js
    configuration.js

Inside index.js

var configuration = require('configuration');

Inside configuration.js:

module.exports = function() {
   return { foo : true };
}

Inside the gulp file I would have:

require('./configuration');
require('../gulp-framework');

However running gulp just leaves me with an error

'Cannot find module configuration'.

I guess all I'm trying to do is pass in a config object to the global framework but not having much luck.

Ideas?


Solution

  • Basically using require('configuration') in index.js will look for the configuration module in /gulp-framework/node_modules/. It will not load the configuration.js from project-a or project-b. (Read more on the exact logic of how require() works here.)

    You need to explicitly pass the configuration and gulp object from your gulpfile.js to index.js:

    In each gulpfile.js:

    var config = require('./configuration.js');
    var gulp = require('../gulp-framework')(require('gulp'), config);
    
    // project-specific stuff using gulp and config goes here
    

    In index.js:

    module.exports = function(gulp, config) {
      // project-independent stuff using gulp and config goes here
    
      return gulp;
    };