Gulp 错误:events.js:72 [英] Gulp error: events.js:72

查看:12
本文介绍了Gulp 错误:events.js:72的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been trying out (or trying to get working) a jekyll style guide from: https://github.com/davidhund/jekyll-styleguide#user-content-requirements

我的 gulpfile 是:

My gulpfile is:

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 我都会得到:

Whenever I run gulp I just get:

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)

推荐答案

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

The problem you are facing is that you are not handling error, so, when gulp finds an error, it throw it, but "nobody" is taking care of it, which causes gulp to break.

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

In order to keep executing gulp, you have to define your error handlers and do whatever you want to do with error, typically, print on the cli what is going on.

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

You also need to identify which part of your code is "throwing" the error, in your case, its caused by the "watchers": a watcher listen for additional events or to add files to the watch. So, a watcher is throwing the error.

你必须抓住它!

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

Add an on handler event after the execution of plugins, and pass this error to a function that will pint it (or something else), but will not break "the watch" (John Snow will be proud) and allows you to identify the error, fix it, at keep watching without restarting gulp manually.

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

PS: Don't forgot to define "the catcher function" !

您的代码可能是这样的:

Your code could be something like this:

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).

Note the error handler definition at the end and the addition of .on('error', errorHandler) on your sass task.

这篇关于Gulp 错误:events.js:72的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆