9

When running gulp on server it doesn't exit after finishing tasks. This is what I get:

[01:16:02] Using gulpfile /test/test/gulpfile.js
[01:16:02] Starting 'clean'...
[01:16:02] Finished 'clean' after 11 ms
[01:16:02] Starting 'default'...
[01:16:02] Starting 'styles'...
[01:16:02] Starting 'scripts'...
[01:16:02] Starting 'watch'...
[01:16:02] Finished 'watch' after 20 ms
[01:16:02] Finished 'default' after 40 ms
[01:16:03] gulp-notify: [Gulp notification] Styles task complete
[01:16:03] Finished 'styles' after 338 ms
[01:16:03] gulp-notify: [Gulp notification] Scripts task complete
[01:16:03] Finished 'scripts' after 921 ms

It stucks there and doesn't get back to command prompt. And this is my gulpfile.js:

var gulp = require('gulp'),
autoprefixer = require('gulp-autoprefixer'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
livereload = require('gulp-livereload'),
minifyCss = require('gulp-minify-css'),
del = require('del');

 gulp.task('styles', function() {
  return gulp.src('./test/css/*.css') 
.pipe(autoprefixer('last 2 version'))
.pipe(concat('main.css')) 
.pipe(gulp.dest('./dist/styles')) 
.pipe(rename({ suffix: '.min' })) 
.pipe(minifyCss())
.pipe(gulp.dest('./dist/styles'))
.pipe(notify({ message: 'Styles task complete' }));
 });

  gulp.task('scripts', function() {
 return gulp.src('./test/js/*.js')
.pipe(concat('main.js'))
.pipe(gulp.dest('./dist/scripts'))
.pipe(rename({ suffix: '.min' }))
.pipe(uglify())
.pipe(gulp.dest('./dist/scripts'))
.pipe(notify({ message: 'Scripts task complete' }));
 });


gulp.task('clean', function() {
 return del(['dist/styles', './dist/scripts']);
 });


gulp.task('default', ['clean'], function() {
gulp.start('styles', 'scripts', 'watch');
 });

gulp.task('watch', function() {
gulp.watch('./test/css/*.css', ['styles']);
gulp.watch('./test/*.js', ['scripts']);

});

The only way to get back to command prompt is with Ctrl+C.

1 Answer 1

18

If you are running the default gulp task (by just running gulp at the command line with no arguments), then it doesn't return because your default task calls your watch task.

gulp.task('watch', function() {
  gulp.watch('./test/css/*.css', ['styles']);
  gulp.watch('./test/*.js', ['scripts']);
}); 

This task will watch the specified locations waiting for changes, and when changes occur, will run the specified tasks. It does not return on its own. You must manually interrupt it.

If you want to create another task that just builds your code and then exits, you can use the following:

gulp.task('build', ['clean', 'styles', 'scripts']);

And then run it with the command:

gulp build
Sign up to request clarification or add additional context in comments.

2 Comments

doh! (facepalm) to me. So obvious yet when you are new to this, everything seems to be a big problem.
I have been stuck for THREE DAYS trying to figure out why gulp keeps failing. This answer finally saved me. I was losing my damn mind. AWS CodeBuild DOES NOT tell you that your builds are halting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.