cosmo0 cosmo0 - 2 months ago 6
Node.js Question

Create node module "xxx is not a function"

I am creating a NW.js app.

I have created a node "sub-module" (not sure how it should be called)

update.js
:

function Updater() {
if (!(this instanceof Updater)) return new Updater();
console.log("init");
}

Updater.prototype.CheckUpdate = function() {
console.log("Checking for update");
};

Updater.prototype.SetTimer = function() {
console.log("set timer");
};

Updater.prototype.destroy = function destroy() {
this.remove();
return true;
};

module.exports = Updater;


I'm calling it like this in the main script,
run.js
:

var updater = require('./update');
updater.CheckUpdate();
updater.SetTimer();


But I get an error in my log file (not show above):


"TypeError: updater.CheckUpdate is not a function"


I'm not sure what I'm doing wrong here...

Answer

CheckUpdate and SetTimer are properties of Updater.prototype , not Updater. So try this:

var Updater = require('./update');
var updater = new Updater();
updater.CheckUpdate();
updater.SetTimer();
Comments