【问题标题】:Gulp error: events.js:72Gulp 错误:events.js:72
【发布时间】:2014-07-23 13:31:28
【问题描述】:

我一直在尝试(或开始工作)来自: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)

【问题讨论】:

    标签: npm gulp


    【解决方案1】:

    你面临的问题是你没有处理错误,所以,当 gulp 发现错误时,它会抛出它,但是“没有人”处理它,这会导致 gulp 中断。

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

    您还需要确定代码的哪一部分“抛出”错误,在您的情况下,它是由“观察者”引起的:观察者侦听其他事件或将文件添加到观察者。因此,观察者正在抛出错误。

    你必须抓住它!

    在插件执行后添加一个 on handler 事件,并将这个错误传递给一个函数,该函数将 pint(或其他东西),但不会破坏“手表”(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)。

    【讨论】:

    • 我做了同样的事情(错误处理程序),但它不起作用!
    • 如果您需要帮助,您必须提供更多详细信息。也许打开另一个问题?或通过聊天进行
    • 老话题,但也许有人会发现它很有用 - 错误处理必须在可以生成它的函数之后,如果你有更多可能失败的管道,每个管道都必须有自己的错误处理
    猜你喜欢
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多