Gulp错误:events.js:72

时间:2014-06-03 09:16:39

标签: npm gulp

我一直在尝试(或尝试工作)来自https://github.com/davidhund/jekyll-styleguide#user-content-requirements

的jekyll风格指南

我的gulpfile是:

var gulp = require('gulp');
var sass = require('gulp-ruby-sass');
var autoprefixer = require('gulp-autoprefixer');
var browserSync = require('browser-sync');
var rename = require('gulp-rename');
var concat = require('gulp-concat');
var minifycss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var clean = require('gulp-clean');
var notify = require('gulp-notify');
var plumber = require('gulp-plumber');

// Handy file paths
paths = {
    scss: "./static/scss/",
    css:  "./static/css/",
    img:  "./static/img/",
    js:   "./static/js/"
}

// SASS
gulp.task('sass', function() {
    // Be specific in what file to process
    return gulp.src(paths.scss+'app.scss')
        .pipe(sass({ style: 'expanded' }))
        .pipe(autoprefixer('> 5%', 'last 2 version', 'ie 9'))
        .pipe(minifycss())
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(paths.css))
        // .pipe(gulp.dest('./_site/static/css/'))
        // .pipe(notify({ message: 'Styles task complete' }));
});

// COPY CSS
gulp.task('copycss', function() {
    return gulp.src(paths.css+'app.min.css')
        .pipe(gulp.dest('./_site/static/css/'))
        // .pipe(notify({ message: 'Copied Minified CSS to _site/static/css' }));
});


// JEKYLL
// Start a `jekyll build` task
// From: http://stackoverflow.com/questions/21293999/use-jekyll-with-gulp
gulp.task('jekyll-build', function() {
    require('child_process').spawn('jekyll', ['build', '--config=_config.dev.yml'], {stdio: 'inherit'});
});

// Start a `jekyll build --watch` task
gulp.task('jekyll-watch', function() {
    require('child_process').spawn('jekyll', ['build', '--watch', '--config=_config.dev.yml'], {stdio: 'inherit'});
});

// BROWSER-SYNC
gulp.task('browser-sync', function() {
    // reload when Jekyll-generated files change
    browserSync.init(['./_site/static/**/*.css', './_site/**/*.html'], {
        server: {
            baseDir: './_site/'
        }
    });
});

// WATCH
gulp.task('watch', function() {
    // TEST: [Only] Run `jekyll build` when I update (the version in) settings.yml
    // gulp.watch('./_config.yml', ['jekyll']);

    // Run Sass when I update SCSS files
    gulp.watch(paths.scss+'**/*.scss', ['sass', 'copycss']);
    // gulp.watch(paths.js+'**/*.js', ['scripts']);
    // gulp.watch(paths.img+'**/*', ['images']);
});


// DEFAULT task
gulp.task('default', ['jekyll-watch', 'watch','browser-sync']);

每当我跑gulp时,我就得到:

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

1 个答案:

答案 0 :(得分:19)

你面临的问题是你没有处理错误,因此,当gulp发现错误时,它会抛出它,但是" nobody"正在照顾它,导致吞咽破裂。

为了继续执行gulp,你必须定义你的错误处理程序并做任何你想做的错误,通常,在cli上打印正在发生的事情。

您还需要确定代码的哪一部分是"投掷"在您的情况下,错误是由"观察者引起的:观察者收听其他事件或向监视添加文件。所以,观察者正在抛出错误。

你必须抓住它!

在执行插件后添加一个on handler事件,并将此错误传递给将要对其进行处理的函数(或其他内容),但不会破坏"手表" (John Snow会感到自豪)并允许您识别错误,修复错误,继续观看而无需手动重启gulp。

PS:别忘了定义捕手功能" !

您的代码可能是这样的:

var gulp = require('gulp');
var sass = require('gulp-ruby-sass');
var autoprefixer = require('gulp-autoprefixer');
var browserSync = require('browser-sync');
var rename = require('gulp-rename');
var concat = require('gulp-concat');
var minifycss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var clean = require('gulp-clean');
var notify = require('gulp-notify');
var plumber = require('gulp-plumber');

// Handy file paths
paths = {
    scss: "./static/scss/",
    css:  "./static/css/",
    img:  "./static/img/",
    js:   "./static/js/"
}

// SASS
gulp.task('sass', function() {
    // Be specific in what file to process
    return gulp.src(paths.scss+'app.scss')
        .pipe(sass({ style: 'expanded' })).on('error', errorHandler)
        .pipe(autoprefixer('> 5%', 'last 2 version', 'ie 9'))
        .pipe(minifycss())
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(paths.css))
        // .pipe(gulp.dest('./_site/static/css/'))
        // .pipe(notify({ message: 'Styles task complete' }));
});

// COPY CSS
gulp.task('copycss', function() {
    return gulp.src(paths.css+'app.min.css')
        .pipe(gulp.dest('./_site/static/css/'))
        // .pipe(notify({ message: 'Copied Minified CSS to _site/static/css' }));
});


// JEKYLL
// Start a `jekyll build` task
// From: http://stackoverflow.com/questions/21293999/use-jekyll-with-gulp
gulp.task('jekyll-build', function() {
    require('child_process').spawn('jekyll', ['build', '--config=_config.dev.yml'], {stdio: 'inherit'});
});

// Start a `jekyll build --watch` task
gulp.task('jekyll-watch', function() {
    require('child_process').spawn('jekyll', ['build', '--watch', '--config=_config.dev.yml'], {stdio: 'inherit'});
});

// BROWSER-SYNC
gulp.task('browser-sync', function() {
    // reload when Jekyll-generated files change
    browserSync.init(['./_site/static/**/*.css', './_site/**/*.html'], {
        server: {
            baseDir: './_site/'
        }
    });
});

// WATCH
gulp.task('watch', function() {
    // TEST: [Only] Run `jekyll build` when I update (the version in) settings.yml
    // gulp.watch('./_config.yml', ['jekyll']);

    // Run Sass when I update SCSS files
    gulp.watch(paths.scss+'**/*.scss', ['sass', 'copycss']);
    // gulp.watch(paths.js+'**/*.js', ['scripts']);
    // gulp.watch(paths.img+'**/*', ['images']);
});


// DEFAULT task
gulp.task('default', ['jekyll-watch', 'watch','browser-sync']);

// Handle the error
function errorHandler (error) {
  console.log(error.toString());
  this.emit('end');
}

请注意最后的错误处理程序定义以及在您的sass任务中添加.on(' error',errorHandler)。