Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js <Class> is not a constructor

I'm getting "HttpHandlers is not a constructor" error when trying to instantiate that class using "new".

Class being instantiated (../lib/restifyHandlers/HttpHandlers):

var config = require('config');
module.exports.config = config;

var util = require('util');
var _ = require('underscore');
var EventEmitter = require("events").EventEmitter;

var HttpHandlers  = function(eventHandlers) {
    var _self = this;
    this.name = "HttpHandlers";
    if (!(this instanceof HttpHandlers)) {
        return new HttpHandlers(eventHandlers);
    }
}

util.inherits(HttpHandlers, EventEmitter);

HttpHandlers.prototype.extractHttpHandlersRequest = function(req, res, next) {
    var _self = this;
    req.locals = {};
    res.locals = {};

}
module.exports.HttpHandlers = HttpHandlers;

Code making the call:

var HttpHandlers = require('../lib/restifyHandlers/HttpHandlers');
var obj = new HttpHandlers(oneRouteConfig.eventHandlers);

Stacktrace:

2016-09-10T23:44:41.571-04:00 - [31merror[39m: Sun, 11 Sep 2016 03:44:41 GMT Worker #master: exiting from error:  TypeError: HttpHandlers is not a constructor 
TypeError: HttpHandlers is not a constructor
    at setupRestifyRoute (/usr/apps/das/src/myrepo/nodejs/myapp/lib/router.js:78:14)
    at Router.setup_routes (/usr/apps/das/src/myrepo/nodejs/myapp/lib/router.js:40:3)
    at /usr/apps/das/src/myrepo/nodejs/myapp/bin/server.js:222:14
    at initialize (/usr/apps/das/src/myrepo/nodejs/myapp/bin/server.js:38:9)
    at setup_server (/usr/apps/das/src/myrepo/nodejs/myapp/bin/server.js:107:4)
    at /usr/apps/das/src/myrepo/nodejs/myapp/bin/server.js:275:4
    at /usr/apps/das/src/myrepo/nodejs/myapp/node_modules/temp/lib/temp.js:231:7
    at FSReqWrap.oncomplete (fs.js:123:15)
like image 984
user994165 Avatar asked Sep 11 '16 03:09

user994165


People also ask

How do you fix not a constructor?

We tried to instantiate a value that is not a constructor as a constructor, which caused the error. To solve the "TypeError: 'X' is not a constructor" in JavaScript, make sure to only use the new operator on valid constructors, e.g. classes or constructor functions.

Is not a constructor node JS error?

The JavaScript exception "is not a constructor" occurs when there was an attempt to use an object or a variable as a constructor, but that object or variable is not a constructor.

Which of the following is not a constructor?

2. Which of the following is not a type of Constructor? Explanation: Friend function is not a constructor whereas others are a type of constructor used for object initialization.

Which of the following is not a built-in constructor in JavaScript?

Javascript has provided some built-in constructors. Those built-in functions include object(), string(), number, etc. We cannot include Math object in those built-in constructors because Math is a global object. The 'new' keyword cannot be used with Math.


3 Answers

When you assigned this:

exports.HttpHandlers = HttpHandlers;

You would need to match that with this:

var HttpHandlers = require('../lib/restifyHandlers/HttpHandlers').HttpHandlers;

You are assigning a property of your module to be .HttpHandlers, not assigning the whole module so if you want that property, you have to reference the property. If you want it to work the other way, you could change to this:

exports = HttpHandlers;

And, then your require() could work the way you are doing it like this:

var HttpHandlers = require('../lib/restifyHandlers/HttpHandlers');
like image 165
jfriend00 Avatar answered Oct 16 '22 12:10

jfriend00


I got this error when calling new ClassName(); and it was caused in the ClassName class by missing off the "module." from module.exports = ClassName

Just in case someone else is as daft as me...

like image 23
Mr Benn Avatar answered Oct 16 '22 12:10

Mr Benn


I got this error because I had some cyclic import. So the class was undefined..

If it can help someone else !

like image 4
Ser Avatar answered Oct 16 '22 10:10

Ser