1

I want to run an index.js file as a part of my gulp process. I could add node index.js through something like gulp-shell as described in this thread (How to run bash commands in gulp?) but it seems there should be an easier way.

I figured there would be a plugin called gulp-node or something to do this but it doesn't exist. How do others do it without turning your initial script into a gulp plugin, which seems intrusive?

Community
  • 1
  • 1
mhkeller
  • 713
  • 1
  • 8
  • 19

1 Answers1

3

You can use var data = require('./path/to/index') to load/run the index.js module in your Gulp task and send any exported information to data. You can read more about require and Node.js modules here.

rgajrawala
  • 2,148
  • 1
  • 22
  • 35
  • 1
    This doesn't work if you put that task within `gulp.watch`. – mhkeller Oct 13 '15 at 16:09
  • @mhkeller Yes, because `require` caches. You'll need to [delete the file from the cached files list](http://stackoverflow.com/a/14801711). – rgajrawala Oct 13 '15 at 17:01
  • Got it. Seems like a good candidate then for a gulp plugin to handle all that code then, unless there's some other solution. – mhkeller Oct 13 '15 at 21:54
  • [Gulp-load](https://www.npmjs.com/package/gulp-load) looks like something that could be useful to you. – rgajrawala Oct 13 '15 at 21:56