util.inherits抛出TypeError:无法读取undefined的属性'prototype'

时间:2013-03-12 06:31:20

标签: node.js inheritance

app.js

var Website = new require( './jslib/website.js' );

./ jslib / website.js

var util = require('util'),
    events = require('events');

module.exports = Website;
function Website()
{
    events.EventEmitter.call( this );
    return this;
}
util.inherits( Website, events.EventEmiter );

控制台输出

PATH_TO_APPDIR>node app.js

util.js:538
  ctor.prototype = Object.create(superCtor.prototype, {
                                          ^
TypeError: Cannot read property 'prototype' of undefined
    at Object.exports.inherits (util.js:538:43)
    at Object.<anonymous> (PATH_TO_APPDIR\jslib\website.js:9:6)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at new require (module.js:378:17)
    at Object.<anonymous> (PATH_TO_APPDIR\app.js:1:16)
    at Module._compile (module.js:449:26)

这是Windows 7上的NodeJS 8.22 *

1 个答案:

答案 0 :(得分:9)

您的代码中有拼写错误。

util.inherits( Website, events.EventEmiter );

应该是

util.inherits( Website, events.EventEmitter );