我正在运行gulp 3.6.2,下面的任务是通过在线示例设置的

gulp.task('watch', ['default'], function () {
  gulp.watch([
    'views/**/*.html',        
    'public/**/*.js',
    'public/**/*.css'        
  ], function (event) {
    return gulp.src(event.path)
      .pipe(refresh(lrserver));
  });

  gulp.watch(['./app/**/*.coffee'],['scripts']);
  gulp.watch('./app/**/*.scss',['scss']);
});

每当我的咖啡脚本中出现错误时,手表就会停止——显然不是我想要的.

As recommended elsewhere I tried this

gulp.watch(['./app/**/*.coffee'],['scripts']).on('error', swallowError);
gulp.watch('./app/**/*.scss',['scss']).on('error', swallowError);
function swallowError (error) { error.end(); }

但它似乎不起作用.

我做错了什么?


为了回应@Aperçu的回答,我修改了我的swallowError方法,并try 了以下方法:

gulp.task('scripts', function () {
  gulp.src('./app/script/*.coffee')
    .pipe(coffee({ bare: true }))
    .pipe(gulp.dest('./public/js'))
    .on('error', swallowError);
});

重新启动,然后在我的咖啡文件中创建了一个语法错误.同样的问题:

[gulp] Finished 'scripts' after 306 μs

stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^
Error: W:\bariokart\app\script\trishell.coffee:5:1: error: unexpected *
*
^
  at Stream.modifyFile (W:\bariokart\node_modules\gulp-coffee\index.js:37:33)
  at Stream.stream.write (W:\bariokart\node_modules\gulp-coffee\node_modules\event-stream\node_modules\through\index.js:26:11)
  at Stream.ondata (stream.js:51:26)
  at Stream.EventEmitter.emit (events.js:95:17)
  at queueData (W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\map-stream\index.js:43:21)
  at next (W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\map-stream\index.js:71:7)
  at W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\map-stream\index.js:85:7
  at W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\lib\src\bufferFile.js:8:5
  at fs.js:266:14
  at W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\graceful-fs\graceful-fs.js:104:5
  at Object.oncomplete (fs.js:107:15)

推荐答案

您的swallowError函数应该如下所示:

function swallowError (error) {

  // If you want details of the error in the console
  console.log(error.toString())

  this.emit('end')
}

我认为你必须将这个函数绑定到任务的error个事件上,而不是watch任务,因为这不是问题的所在,你应该在每个可能失败的任务上设置这个错误回调,比如当你错过了;或其他东西时,插件会断开,以防止watch任务停止.

例如:

gulp.task('all', function () {
  gulp.src('./app/script/*.coffee')
    .pipe(coffee({ bare: true }))
    .on('error', swallowError)
    .pipe(gulp.dest('./public/js'))

  gulp.src('css/*.scss')
    .pipe(sass({ compass: true }))
    .on('error', swallowError)
    .pipe(cssmin())
    .pipe(gulp.dest('dist'))
})

或者,如果您不介意包含另一个模块,可以使用100log函数来避免在gulpfile中声明额外的函数:

.on('error', gutil.log)

但我可能会建议大家看一下awesome100插件,它用于删除error事件的onerror处理程序,导致流中断.它的使用非常简单,并且可以阻止您捕获所有可能失败的任务.

gulp.src('./app/script/*.coffee')
  .pipe(plumber())
  .pipe(coffee({ bare: true }))
  .pipe(gulp.dest('./public/js'))

更多关于这一点的信息由相关插件的创建者在this article上发布.

Node.js相关问答推荐

无法验证叶签名|无法验证第一个证书

使用OpenAI API时遇到问题

当建议在第二代上运行云功能时,现在是否有新的Firestore AdminClient可供使用?

MongoDB如果文档S的字段小于另一个字段,则将该字段加一

Nestjs重写子类dto nodejs中的属性

Node.js中使用ffmpeg如何获取视频截图的位置?

找不到模块bcryptjs

如何通过node下载zip并直接解压zip?

使用 fs.createWriteStream 将数据写入 bigquery (node.js) 时出现模式错误

ResponseError:键空间ks1不存在

将 express js app.use() 移动到另一个文件

在mongodb中只查询整数

fs.writefile 选项参数的可能值,尤其是只读文件的整数

带权限的机密 Rest-Api - 总是 403 - 我做错了什么?

使用中的端口代码:'EADDRINUSE',即使在 kill 命令之后

AWS Kinesis 中的分区键是什么?

使用restify时如何支持cors

在多个 .env 文件之间切换,例如 .env.development 和 node.js

Node.js 支持 =>(箭头函数)

gyp WARN EACCES 用户root没有访问开发目录的权限