gpt4 book ai didi

javascript - 使用 Node.js v5.10.1 时出现 'inherits' 错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:31:32 24 4
gpt4 key购买 nike

当我尝试运行《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);
};

最佳答案

你可以试试这个。如果这对您有用。

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

关于javascript - 使用 Node.js v5.10.1 时出现 'inherits' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36586204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com