Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Gulp Basics Gulp your JavaScript Workflow Minifying JavaScript Files

Christopher R
Christopher R
1,441 Points

events.js:160 throw er; // Unhandled 'error' even

I'm getting the above error in the console when running gulp minifyScripts:

"use strict";

var gulp = require('gulp'),
  concat = require('gulp-concat'),
  uglify = require('gulp-uglify'),
  rename = require('gulp-rename');

gulp.task("concatScripts", function() {
    gulp.src([
        'js/jquery.js',
        'js/sticky/jquery.sticky.js',
        'js/main.js'
        ])
    .pipe(concat('app.js'))
    .pipe(gulp.dest('js'));
});

gulp.task("minifyScripts", function() {
    gulp.src("js/app.js")
    .pipe(uglify())
    .pipe(rename('app.min.js'))
    .pipe(gulp.dest('js'));
});

gulp.task("default", ["hello"], function() {
    console.log("the default task!!!!");
});

2 Answers

I think that uglify might be having an error, which is currently not being handled. If you try adding this to the uglify step you might be able to learn more about the nature of your error:

.pipe(uglify().on('error', function(e){
            console.log(e);
         }))
gulp.task("minifyScripts", function() {
    gulp.src("js/app.js")
    .pipe(uglify().on('error', function(e){
       console.log(e);
     }))
    .pipe(rename('app.min.js'))
    .pipe(gulp.dest('js'));
});

Oh, I think I know where you went wrong. You are running minifyScripts but not the concatScripts task. Adding the concat task to your minify task could be the solution.

gulp.task("minifyScripts", ["concatScripts"], function() {...

If you pass an array of strings that match other tasks you have declared as the second argument in the gulp.task method then those will be run before that task.