5

I am using gulp to copy and clean my front-end projects. I want to run some sequential tasks. It means, for example, I want the copy task to be started and finished and then run its the following task (a copy task). I frequently see this error when I run my tasks:

events.js:72
    throw er; // Unhandled 'error' event
          ^
Error: ENOENT, lstat '/home/reza/projects/account-web/dist/views'

After lstat, each I see a file name randomly. I just find that when there is no dist folder or it is empty copy tasks run properly but when the dist folder is not empty even clean task throws and error. There are my gulp file and command results below:

var gulp = require('gulp');
var clean = require('gulp-clean');
var merge = require('gulp-merge');
var runSequence = require('run-sequence');

gulp.task('clean', function () {
    gulp.src('./dist/**').pipe(clean());
});

gulp.task('copy-js', function () {
    return gulp.src('app/js/**').pipe(gulp.dest('dist/js/'));
});
gulp.task('copy-bower', function () {
    return     gulp.src('bower_components/**').pipe(gulp.dest('dist/bower_components/'));
});
gulp.task('copy-script', function () {
    return     gulp.src('app/scripts/**').pipe(gulp.dest('dist/scripts/'));
});
gulp.task('copy-styles', function () {
    var stream1 =     gulp.src('app/styles/**').pipe(gulp.dest('dist/styles/'));
    var stream2 =     gulp.src('app/fonts/**').pipe(gulp.dest('dist/fonts/'));
    var stream3 = gulp.src('app/img/**').pipe(gulp.dest('dist/img/'));
    return merge(stream1, stream2, stream3);
});
gulp.task('copy-views', function () {
    var stream1 =     gulp.src('app/views/**').pipe(gulp.dest('dist/views/'));
    var stream2 = gulp.src('app/*').pipe(gulp.dest('dist/'));
    return merge(stream1, stream2);
});

gulp.task('dist', function () {
    runSequence(['copy-bower','copy-js', 'copy-script', 'copy-styles',     'copy-views']);//
});

gulp.task('dev', function () {
    runSequence('clean', 'dist')
});

gulp.task('default', ['dev']);

I thought that there is concurrency problem and the tasks are not running really sequentially, so I tries some modules to solve this problem like runSequence.

➜  account-web git:(master) ✗ gulp clean
[14:20:55] Using gulpfile ~/projects/account-web/Gulpfile.js
[14:20:55] Starting 'clean'...
[14:20:55] Finished 'clean' after 5.58 ms

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: ENOENT, lstat '/home/reza/projects/account-web/dist/views'


➜  account-web git:(master) ✗ gulp dev
[14:35:03] Using gulpfile ~/projects/account-web/Gulpfile.js
[14:35:03] Starting 'dev'...
[14:35:03] Starting 'clean'...
[14:35:03] Finished 'clean' after 5.47 ms
[14:35:03] Starting 'dist'...
[14:35:03] Starting 'copy-bower'...
[14:35:03] Starting 'copy-js'...
[14:35:03] Starting 'copy-script'...
[14:35:03] Starting 'copy-styles'...
[14:35:03] Starting 'copy-views'...
[14:35:03] Finished 'dist' after 14 ms
[14:35:03] Finished 'dev' after 22 ms

stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^
Error: ENOENT, open '/home/reza/projects/account-    web/dist/styles/fonts.css'
RezKesh
  • 2,861
  • 2
  • 24
  • 35
  • possible duplicate of [Gulp error: events.js:72](http://stackoverflow.com/questions/24011349/gulp-error-events-js72) – Avalanche Aug 09 '15 at 10:27
  • @Avalanche his problem is "spawn ENOENT" but mine is "Error: ENOENT, open " – RezKesh Aug 09 '15 at 11:09
  • 1
    hmm... it still looks to me that you have not handled some errors. Lstat seems to be an asynchronous operation, according to the [documentation](https://nodejs.org/api/fs.html#fs_fs_lstat_path_callback) -- this could potentially cause some trouble. – Avalanche Aug 09 '15 at 11:23
  • @Avalanche But do you why does is raise lstat? Is it a way to avoid the error? I just did what the post says about error handling but I still see same error! – RezKesh Aug 09 '15 at 11:30
  • I don't remember having such issues, but it does look that your gulp conf is getting into some errors when compiling your files; this is why your dist folder is either empty or not there. – Avalanche Aug 09 '15 at 11:45

1 Answers1

7

I wonder if it's attempting to asynchronously delete a file within a nested folder after the nested folder has already been deleted. Try changing your src to include the dist folder without the wildcards. This will tell it to delete the dist folder instead of each item independently.

gulp.task('clean', function () {
    gulp.src('./dist').pipe(clean());
});

It also appears that gulp-clean has been deprecated, you may want to checkout del as a replacement. It is able to handle your original glob pattern correctly.

John Miller
  • 999
  • 1
  • 9
  • 15
  • Wrote a small post talking about this issue [here](http://www.gembalabs.com/2015/09/16/gulp-clean-error-enoent-lstat/). – John Miller Sep 16 '15 at 13:59
  • 1
    The clean task should returns the stream or it could run in parallel of other tasks. – Dinoboff Mar 22 '16 at 12:01
  • 1
    If you use 'del' instead of 'clean' use 'del.sync'. 'del' returns a promise so it runs asynchronously which is why the OP had the problem with the file getting deleted (cleaned) when another task was running. – nmgeek Nov 20 '16 at 20:42