12

I'm trying to build a jekyll site using Gulp.js. I've read that I shouldn't use a plugin in this question.

I've been investigating using a child process as suggested but I keep getting an error:

events.js:72
    throw er; // Unhandled 'error' event
          ^
Error: spawn ENOENT
    at errnoException (child_process.js:988:11)
    at Process.ChildProcess._handle.onexit (child_process.js:779:34)

Here's my gulp file:

var gulp = require('gulp');
var spawn = require('child_process').spawn;
var gutil = require('gulp-util');

gulp.task('jekyll', function (){
    spawn('jekyll', ['build'], {stdio: 'inherit'});
});

gulp.task('default', ['jekyll']);

What am I doing wrong? I'm on Node 0.10.25, Win 7.

EDIT I've had a google around ENOENT errors previously. Have checked my path and Ruby is there and I can run jekyll from the command line. Still no joy.

Community
  • 1
  • 1
matthewbeta
  • 696
  • 1
  • 7
  • 17

3 Answers3

15

I also had this problem and found the answer to using spawn.

The problem is with how Node finds executables on Windows. For more details look at this answer:

https://stackoverflow.com/a/17537559

In short, change spawn('jekyll', ['build']) to spawn('jekyll.bat', ['build']) in order to get spawn to work.

Community
  • 1
  • 1
andrewh
  • 324
  • 4
  • 3
  • SpikeMeister, I just saw your answer by googling for my own problem. Sorry didn't accept and vote it up earlier. Do you know if the .bat fix works on Mac/Unix machines? Made my day! – matthewbeta Sep 08 '14 at 10:16
  • 2
    Re my previous comment. You can set the command based on the OS: `var jekyll = process.platform === "win32" ? "jekyll.bat" : "jekyll";` then just run: `spawn(jekyll' ['build'])` – matthewbeta Sep 08 '14 at 10:20
9

So I ended up using exec instead. Here's my gulp file:

var gulp = require('gulp');
var exec = require('child_process').exec;
var gutil = require('gulp-util');

gulp.task('jekyll', function (){
exec('jekyll build', function(err, stdout, stderr) {
    console.log(stdout);
});
});

Here's a post about the differences

matthewbeta
  • 696
  • 1
  • 7
  • 17
1

A few years later, you can now use cross-spawn, which takes care of cross-platform issues with node's spawn. It's a straight replacement for Node's spawn.

Install to your project with

npm install cross-spawn

Then add

const spawn = require('cross-spawn');

to your script.

Then in your gulp script use as normal:

gulp.task('jekyll', function (){
    spawn('jekyll', ['build'], {stdio: 'inherit'});
});

If you're using this in a non-gulp script:

var jekyll = spawn('bundle exec jekyll build');
Arthur
  • 351
  • 1
  • 6