Search code examples
javascriptnode.jsgulppipelinegulp-uglify

gulp generates TypeError: The Streams property must be of type function


I got an error TypeError: The "streams[stream.length - 1]" property must be of type function. Received an instance of Pumpify while trying to minify a javascript using gulp package ,

Using gulpfile MY_PROJECT_PATH\gulpfile.js
Starting 'compress'...
'compress' errored after 21 ms
TypeError: The "streams[stream.length - 1]" property must be of type function. Received an instance of Pumpify
at popCallback (MY_PROJECT_PATH\node_modules\readable-stream\lib\internal\streams\pipeline.js:59:3)
at pipeline (MY_PROJECT_PATH\node_modules\readable-stream\lib\internal\streams\pipeline.js:134:37

This is my code in [gulpfile.js]

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var pipeline = require('readable-stream').pipeline;

gulp.task('compress', function () {
   return pipeline(
       gulp.src('DIR_NAME/*.js'),
       uglify(),
       gulp.dest('DIR_NAME/dist')
   );
});

The package.json file: I tried to install [pipeline, readable-stream, pumpify] while debugging

{
  "devDependencies": {
     "gulp": "^4.0.2",
     "gulp-uglify": "^3.0.2",
     "pipeline": "^0.1.3",
     "pumpify": "^2.0.1",
     "readable-stream": "^4.3.0"
    }
}

Solution

  • Solution 1

    The pipeline function from stream or readable_stream expects a callback as a last parameter.

    var gulp = require('gulp');
    var uglify = require('gulp-uglify');
    var pipeline = require('readable-stream').pipeline;
    
    gulp.task('compress', function (cb) {
        return pipeline(
            gulp.src('DIR_NAME/*.js'),
            uglify(),
            gulp.dest('DIR_NAME/dist'),
            cb
        );
    });
    

    Solution 2

    stream/promises exposes a promise-based version of pipeline which does not use a callback:

    var gulp = require('gulp');
    var uglify = require('gulp-uglify');
    var pipeline = require('stream/promises').pipeline;
    
    gulp.task('compress', async function () {
        await pipeline(
            gulp.src('DIR_NAME/*.js'),
            uglify(),
            gulp.dest('DIR_NAME/dist')
        );
    });
    

    Solution 3

    Then there is the tradition way of piping steams, with the .pipe() method.

    var gulp = require('gulp');
    var uglify = require('gulp-uglify');
    
    gulp.task('compress', function () {
        return gulp.src('DIR_NAME/*.js')
            .pipe(uglify())
            .pipe(gulp.dest('DIR_NAME/dist'));
    });