title: Why TypeError: stream.on is not a function
body: <p>I have been working on an Angular JS based application. I have written a set of gulp commands to uglify/remove comments from JS code. Since I wanted to trace the exceptions properly, I wrapped the tasks with 'pump' (please see the code) and installed 'pump' through power shell.
  Finally when I try to run the gulp command it shows me this error. Googling on this did not work for me. Please advice me.</p>
  
  <p><strong>Gulpfile</strong></p>
  
  <pre><code>// include plug-ins
  'use strict';
  var gulp = require('gulp');
  var concat = require('gulp-concat');
  var htmlbuild = require('gulp-htmlbuild');
  var plugins = require('gulp-load-plugins')();
  var es = require('event-stream');
  var clean = require('gulp-clean');
  var uglify = require('gulp-uglify');
  var strip = require('gulp-strip-comments');
  var pump = require('pump');
  
  // pipe a glob stream into this and receive a gulp file stream 
  var gulpSrc = function (opts) {
      var paths = es.through();
      var files = es.through();
      paths.pipe(es.writeArray(function (err, srcs) {
          var fixedFiles = [];
          for (var i=0; i &lt; srcs.length; i++)
          {
              fixedFiles[i] = srcs[i].replace("~", ".");
              console.log(fixedFiles[i]);
          }
          gulp.src(fixedFiles, opts).pipe(files);
      }));
  
      return es.duplex(paths, files);
  };
  
  var jsBuild = es.pipeline(
    plugins.concat('all.js'),
    gulp.dest('./Scripts/')
  );
  
  gulp.task('clean', function () {
      return pump(gulp.src('./Scripts/all.js')
              .pipe(clean({ force: true })));
  });
  
  gulp.task('build', function () {
      return pump(gulp.src(['./Views/Home/Layout.cshtml'])
      .pipe(htmlbuild({
          js: htmlbuild.preprocess.js(function (block) {
              block.pipe(gulpSrc())
             .pipe(jsBuild);
        })
      }))
      );
  });
  
  // Minify 
  gulp.task('minify', function () {
      return pump (gulp.src(['./Scripts/all.js']).
      pipe(uglify()).
      pipe(gulp.dest('./testing')));
  });
  
  // Strip Comments
  gulp.task('StripComments', function () {
      return pump(gulp.src(['./Scripts/all.js'])
        .pipe(strip())
        .pipe(gulp.dest('./testing')));
  });
  
  gulp.task(pump('default', ['clean', 'build', 'StripComments'], function () { }));
  </code></pre>
  
  <p><strong>Power-shell</strong></p>
  
  <pre><code>PS C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain&gt; gulp
  C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain\node_modules\pump\index.js:24
    stream.on('close', function () {
           ^
  
  TypeError: stream.on is not a function
      at destroyer (C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain\node_modules\pump\index.js:24:10)
      at C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain\node_modules\pump\index.js:68:12
      at Array.map (native)
      at pump (C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain\node_modules\pump\index.js:65:26)
      at Object.&lt;anonymous&gt; (C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain\gulpfile.js:66:11)
      at Module._compile (module.js:570:32)
      at Object.Module._extensions..js (module.js:579:10)
      at Module.load (module.js:487:32)
      at tryModuleLoad (module.js:446:12)
      at Function.Module._load (module.js:438:3)
  </code></pre>
  
  <p><strong>C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain\node_modules\pump\index.js</strong></p>
  
  <pre><code>var once = require('once')
  var eos = require('end-of-stream')
  var fs = require('fs') // we only need fs to get the ReadStream and WriteStream prototypes
  
  var noop = function () {}
  
  var isFn = function (fn) {
    return typeof fn === 'function'
  }
  
  var isFS = function (stream) {
    if (!fs) return false // browser
    return (stream instanceof (fs.ReadStream || noop) || stream instanceof (fs.WriteStream || noop)) &amp;&amp; isFn(stream.close)
  }
  
  var isRequest = function (stream) {
    return stream.setHeader &amp;&amp; isFn(stream.abort)
  }
  
  var destroyer = function (stream, reading, writing, callback) {
    callback = once(callback)
  
    var closed = false
    stream.on('close', function () {
      closed = true
    })
  
    eos(stream, {readable: reading, writable: writing}, function (err) {
      if (err) return callback(err)
      closed = true
      callback()
    })
  
    var destroyed = false
    return function (err) {
      if (closed) return
      if (destroyed) return
      destroyed = true
  
      if (isFS(stream)) return stream.close() // use close for fs streams to avoid fd leaks
      if (isRequest(stream)) return stream.abort() // request.destroy just do .end - .abort is what we want
  
      if (isFn(stream.destroy)) return stream.destroy()
  
      callback(err || new Error('stream was destroyed'))
    }
  }
  
  var call = function (fn) {
    fn()
  }
  
  var pipe = function (from, to) {
    return from.pipe(to)
  }
  
  var pump = function () {
    var streams = Array.prototype.slice.call(arguments)
    var callback = isFn(streams[streams.length - 1] || noop) &amp;&amp; streams.pop() || noop
  
    if (Array.isArray(streams[0])) streams = streams[0]
    if (streams.length &lt; 2) throw new Error('pump requires two streams per minimum')
  
    var error
    var destroys = streams.map(function (stream, i) {
      var reading = i &lt; streams.length - 1
      var writing = i &gt; 0
      return destroyer(stream, reading, writing, function (err) {
        if (!error) error = err
        if (err) destroys.forEach(call)
        if (reading) return
        destroys.forEach(call)
        callback(error)
      })
    })
  
    return streams.reduce(pipe)
  }
  
  module.exports = pump
  </code></pre>