0

I followed this question How to implement inheritance in node.js modules? but it didn't work for me.

// usermgmt.js
function Usermgmt() {
};

module.exports = Usermgmt;
// authentication.js
var Usermgmt = require('./usermgmt');

function Authentication()  {
    Authentication.super_.apply(this, arguments);
}
Authentication.super_ = Usermgmt;

Authentication.prototype = Object.create(Usermgmt.prototype, {
    constructor: {
        value: Authentication,
        enumerable: false
    }
});

Authentication.prototype.test = function() {
    console.log("test");
};

module.exports = Authentication;
// index.js
var auth = new require('../modules/authentication');
auth.test();

i tryed it also with class but it didn't work. nodejs version 12 call method in another method I get the error TypeError: auth.test is not a function

user3807340
  • 143
  • 1
  • 9
  • 1
    `new require(…)` constructs a new `require` instance! You want `const Auth = require(…)`, and then do `new Auth()`. Or `new (require(…))`. – Bergi Apr 23 '20 at 08:41
  • thanks var auth = new (require('../modules/authentication')); auth.test(); worked now – user3807340 Apr 23 '20 at 09:20

0 Answers0