为什么出现 TypeError:stream.on 不是函数

问题描述 投票:0回答:2

我一直在开发基于 Angular JS 的应用程序。我编写了一组 gulp 命令来丑化/删除 JS 代码中的注释。由于我想正确跟踪异常,因此我用“pump”包装了任务(请参阅代码)并通过 power shell 安装了“pump”。 最后,当我尝试运行 gulp 命令时,它向我显示此错误。谷歌搜索这对我来说不起作用。请给我建议。

Gulpfile

// 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 < 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 () { }));

电源壳

PS C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain> 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.<anonymous> (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)

C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain ode_modules\pump\index.js

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)) && isFn(stream.close)
}

var isRequest = function (stream) {
  return stream.setHeader && 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) && streams.pop() || noop

  if (Array.isArray(streams[0])) streams = streams[0]
  if (streams.length < 2) throw new Error('pump requires two streams per minimum')

  var error
  var destroys = streams.map(function (stream, i) {
    var reading = i < streams.length - 1
    var writing = i > 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
javascript angularjs gulp uglifyjs
2个回答
0
投票

gulp.task(pump('default', ['clean', 'build', 'StripComments'], function () { }));

您在这里拨打

pump()
,但没有直播。


-3
投票

你应该像这样定义一个流:

var stream= fs.readStream('filename');

我更喜欢这样定义流:

var stream= fs.createReadStream('filename');
© www.soinside.com 2019 - 2024. All rights reserved.