Search code examples
javascriptnode.jsnw.js

exporting module in node.js and nw.js but unable to call to constructor


I am creating web application using node.js and nw.js Now i am exporting the below module

admin.js

module.exports = function (firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.fullName = function () { 
    return this.firstName + ' ' + this.lastName;
}}

and trying to access it in login.js file

var adm= require('./model/admin.js');
var adms=new adm("hi","wow");    
adms.fullName();

But it says adm is not a constructor


Solution

  • Your code seems great.

    Try maybe to change your admin.js but usualy your code should work

    var adm = function (firstName, lastName) {
       this.firstName = firstName;
       this.lastName = lastName;
       this.fullName = function () {
          return this.firstName + ' ' + this.lastName;
       }
    }
    var exports = module.exports = adm