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

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

问题描述

我一直在尝试(或努力工作)一个jekyll风格指南: https://github.com/davidhund/jekyll-styleguide#user-content-requirements



我的大文件是:

  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');

//方便的文件路径
paths = {
scss:./static/scss/,
css:./static/css/,
img:./static/img/,
js:./static/js/
}

// SASS
gulp .task('sass',function(){
//指定要处理的文件
return gulp.src(paths.scss +'app.scss')
.pipe(sass ({style:'expanded'}))
.pipe(autoprefixer('> 5%','last 2 version','ie 9'))
.pipe(minifycss())
.pipe({(后缀:'.min'}))
.pipe(gulp.dest(paths.css))
// .pipe(gulp.dest('./ _site / static / css /'))
// .pipe(notify({message:'Styles task complete'}));
});
$ b $ //复制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
//启动一个`jekyll build`任务
//来源: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'});
});
$ b $ //启动一个`jekyll build --watch`任务
gulp.task('jekyll-watch',function(){
require('child_process')。spawn ('jekyll',['build','--watch','--config = _config.dev.yml'],{stdio:'inherit'});
});
$ b $ // BROWSER-SYNC
gulp.task('browser-sync',function(){
//当Jekyll生成的文件更改
browserSync时重新加载。 init(['./_ site / static / ** / *。css','./_site/**/*.html'],{
server:{
baseDir:'./_site /'
}
});
});
$ b $ // watch
gulp.task('watch',function(){
// TEST:[Only]更新时运行`jekyll build` )settings.yml
// gulp.watch('./_ config.yml',['jekyll']);

//更新SCSS文件时运行Sass
gulp.watch(paths.scss +'** / *。scss',['sass','copycss']);
// gulp.watch(paths.js +'** / *。js',[ 'scripts']);
// gulp.watch(paths.img +'** / *',['images']);
});

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

每当我运行 gulp 时,我只会得到:

  events.js:72 
throw er; //未处理的'错误'事件
^
错误:在errnoException(child_process.js:998:11)处产生ENOENT
Process.ChildProcess._handle.onexit(child_process。 js:789:34)


解决方案

是因为你没有处理错误,所以,当大嘴发现错误时,它会抛出错误,但是没有人会照顾它,这会导致吞咽困难。



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

您还需要确定代码的哪个部分抛出了错误,在您的情况下,它是由观察者引起的:观察者监听附加事件或向手表添加文件。所以,一个观察者正在抛出错误。



你必须抓住它!



在执行插件后添加一个on处理程序事件, (或其他)的功能错误,但不会打破手表(John Snow将引以为豪),并允许您识别错误,修正错误,继续观看,而无需手动重新启动吞咽。 p>

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');

//方便的文件路径
paths = {
scss:./static/scss/,
css:./static/css/,
img:./static/img/,
js:./static/js/
}

// SASS
gulp .task('sass',function(){
//指定要处理的文件
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({(后缀:'.min'}))
.pipe(gulp.dest(paths.css))
//。管道(gulp.dest('./_ site / static / css /'))
// .pipe(notify({message:'Styles task complete'}));
});
$ b $ //复制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
//启动一个`jekyll build`任务
//来源: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'});
});
$ b $ //启动一个`jekyll build --watch`任务
gulp.task('jekyll-watch',function(){
require('child_process')。spawn ('jekyll',['build','--watch','--config = _config.dev.yml'],{stdio:'inherit'});
});
$ b $ // BROWSER-SYNC
gulp.task('browser-sync',function(){
//当Jekyll生成的文件更改
browserSync时重新加载。 init(['./_ site / static / ** / *。css','./_site/**/*.html'],{
server:{
baseDir:'./_site /'
}
});
});
$ b $ // watch
gulp.task('watch',function(){
// TEST:[Only]更新时运行`jekyll build` )settings.yml
// gulp.watch('./_ config.yml',['jekyll']);

//更新SCSS文件时运行Sass
gulp.watch(paths.scss +'** / *。scss',['sass','copycss']);
// gulp.watch(paths.js +'** / *。js',[ 'scripts']);
// gulp.watch(paths.img +'** / *',['images']);
});

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

//处理错误
函数errorHandler(error){
console.log(error.toString());
this.emit('end');



$ b $ p
$ b

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


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

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']);

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)

解决方案

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.

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.

You have to catch it !

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: 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');
}

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天全站免登陆