使用node.js v5.10.1获取'inherits'错误

时间:2016-04-13 00:02:53

标签: javascript node.js

尝试从实践中的Node.js一书中运行一个简单的脚本时出现以下错误。

我不确定导致此错误的原因。我尽力寻找解释,但我做不到。 util.inherit模块在v5.10.1中不起作用吗?还是我错过了别的什么?我是node.js的新手。

~/node-project$ node index.js
util.js:786
    throw new TypeError('The super constructor to `inherits` must not ' +
    ^

TypeError: The super constructor to `inherits` must not be null or undefined.
    at Object.exports.inherits (util.js:786:11)
    at Object.<anonymous> (/Users/byoungdale/node-project/countstream.js:6:6)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (/Users/byoungdale/node-project/index.js:1:81)
    at Module._compile (module.js:413:34)

index.js

var CountStream = require('./countstream.js');
var countStream = new CountStream('book');
var http = require('http');

http.get('http://www.manning.com', function(res) {
  res.pipe(countStream);
});

countStream.on('total', function(count) {
 console.log('Total matches: ', count)
});

countstream.js

var Writable = require('stream').Writeable;
var util = require('util');

module.exports = CountStream;

util.inherits(CountStream, Writable);

function CountStream(matchText,options) {
  Writeable.call(this, options);
  this.count = 0;
  this.matcher = new RegExp(matchText, 'ig');
}

CountStream.prototype._write = function(chunk, encoding, cb) {
  var matches = chuck.toString().match(this.matcher);
  if (matches) {
   this.count += matches.length;
  }
  cb();
};

CountStream.prototype.end = function() {
  this.emit('total', this.count);
};

1 个答案:

答案 0 :(得分:0)

你可以试试这个。如果这可能适合你。

var可写=要求(&#39;流&#39;)。可写;