运行gulp时理解构建错误

运行gulp时理解构建错误,gulp,Gulp,每次我运行gulp任务时,它都会在第一次运行时生成一个错误。错误如下。当我再次运行它时,它构建时没有任何错误 我很困惑,为什么它第一次抛出错误,但不是在那之后 [08:38:08] Using gulpfile ~/Documents/Code/proj2/Gulpfile.js [08:38:08] Starting 'stylus'... [08:38:08] Finished 'stylus' after 7.99 ms [08:38:08] Starting 'copyClientHTM

每次我运行gulp任务时,它都会在第一次运行时生成一个错误。错误如下。当我再次运行它时,它构建时没有任何错误

我很困惑,为什么它第一次抛出错误,但不是在那之后

[08:38:08] Using gulpfile ~/Documents/Code/proj2/Gulpfile.js
[08:38:08] Starting 'stylus'...
[08:38:08] Finished 'stylus' after 7.99 ms
[08:38:08] Starting 'copyClientHTML'...
[08:38:08] Finished 'copyClientHTML' after 2.27 ms
[08:38:08] Starting 'clientLint'...
[08:38:08] Starting 'demon'...
[08:38:08] Finished 'demon' after 1.88 ms
[gulp] [nodemon] v1.2.1
[gulp] [nodemon] to restart at any time, enter `rs`
[gulp] [nodemon] watching: *.*
[gulp] [nodemon] starting `node server/js/server.js`
[08:38:08] Starting 'watch'...
[08:38:09] Finished 'watch' after 31 ms

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: ENOENT, stat '/Users/jhans/Documents/Code/proj2/client/build/index.html'
包括Gulpfile

/*jshint globalstrict: true*/
'use strict';

var gulp = require('gulp'),
  browserify = require('gulp-browserify'),
  clean = require('gulp-clean'),
  jshint = require('gulp-jshint'),
  stylish = require('jshint-stylish'),
  stylus = require('gulp-stylus'),
  del = require('del'),
  nodemon = require('gulp-nodemon'),
  concat = require('gulp-concat');

var paths = {
  client: {
    scripts: 'client/js/**/*.js',
    html: 'client/views/*.html',
    index: 'client/*.html',
    css: 'client/css/*.styl',
    conf: 'client/conf.js'
  },
  server: {
    scripts: 'server/js/**/*.js'
  }

};

// Rerun the task when a file changes
gulp.task('watch', function () {
  gulp.watch(paths.client.css, ['stylus']);
  gulp.watch(paths.client.scripts, ['browserify']);
  gulp.watch([paths.client.html, paths.client.index], ['copyClientHTML']);
  gulp.watch(paths.server.scripts, ['serverLint']);
});


gulp.task('demon', function () {
  nodemon({
    script: 'server/js/server.js',
    ext: 'js',
    env: {
      'NODE_ENV': 'development'
    }
  })
    .on('start', ['watch'])
    .on('change', ['watch'])
    .on('restart', function () {
      console.log('restarted!');
    });
});

// Default Task
gulp.task('default', ['build', 'demon']);

gulp.task('build', ['stylus', 'copyClientHTML', 'browserify']);


/********** Building CSS *********/
gulp.task('stylus', function () {

  del(['client/build/css/*']);

  gulp.src(paths.client.css)
    .pipe(stylus())
    .pipe(concat('all.css'))
    .pipe(gulp.dest('client/build/css/'));
});


gulp.task('clientLint', function () {
  return gulp.src([paths.client.scripts])
    .pipe(jshint())
    .pipe(jshint.reporter(stylish));
});


gulp.task('serverLint', function () {
  return gulp.src([paths.server.scripts])
    .pipe(jshint())
    .pipe(jshint.reporter(stylish));
});


gulp.task('browserify',['clientLint'], function () {

  del(['client/build/js/*']);

  gulp.src('client/js/app.js')
    .pipe(browserify({
      insertGlobals: true,
      debug: true
    }))
    .pipe(gulp.dest('client/build/js'));
});


gulp.task('copyClientHTML', function () {
  del(['client/build/views/*.*']);
  del(['client/build/index.html']);

  gulp.src(paths.client.html)
    .pipe(gulp.dest('client/build/views'));

  gulp.src(paths.client.index)
    .pipe(gulp.dest('client/build'));

});

您的问题可能来自copyClientHTML任务,也可能来自其他任务

实际上,您必须知道del模块不是同步运行的。因此,它可能会在创建文件后删除您的文件,并可以解释您遇到的问题

我可能会建议您创建一个专门用于清理文件的任务,您可以将其作为某些任务的依赖项;或者最好使用一个小函数,您可以在其中传递一个参数来清理指定的文件夹


这将确保您在创建新文件之前完成清理。

您的gulpfile是什么样子的?如果没有,这是不可能的。我想这取决于/Users/jhans/Documents/Code/proj2/client/build/index.html的存在,并且第一次运行创建了它。