Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript "Not a Constructor" Exception while creating objects

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.

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.

Can we create class without constructor in JavaScript?

You can define a class without a constructor in JavaScript. If you do not specify a constructor method a default constructor is used. By default, the constructor is defined as: The body of a class is the part that is in curly brackets {} . This is where you define class members, such as methods or constructors.

Can we create constructor in JavaScript?

A constructor is a special function that creates and initializes an object instance of a class. In JavaScript, a constructor gets called when an object is created using the new keyword. The purpose of a constructor is to create a new object and set values for any existing object properties.


The code as posted in the question cannot generate that error, because Project is not a user-defined function / valid constructor.

function x(a,b,c){}
new x(1,2,3);               // produces no errors

You've probably done something like this:

function Project(a,b,c) {}
Project = {};               // or possibly   Project = new Project
new Project(1,2,3);         // -> TypeError: Project is not a constructor

Variable declarations using var are hoisted and thus always evaluated before the rest of the code. So, this can also be causing issues:

function Project(){}
function localTest() {
    new Project(1,2,3); // `Project` points to the local variable,
                        // not the global constructor!

   //...some noise, causing you to forget that the `Project` constructor was used
    var Project = 1;    // Evaluated first
}

An additional cause of this can be ES2015 arrow functions. They cannot be used as constructors.

const f = () => {};
new f(); // This throws "f is not a constructor"

For me it was the differences between import and require on ES6.

E.g.

processor.js

    class Processor {
    
    }
    
    export default Processor

index.js

const Processor = require('./processor');
const processor = new Processor() //fails with the error
    
import Processor from './processor'
const processor = new Processor() // succeed

I've googled around also and found this solution:

You have a variable Project somewhere that is not a function. Then the new operator will complain about it. Try console.log(Project) at the place where you would have used it as a construcotr, and you will find it.


For my project, the problem turned out to be a circular reference created by the require() calls:

y.js:
var x = require("./x.js");
var y = function() { console.log("result is " + x(); }
module.exports = y;

x.js:
var y = require("./y.js");
var my_y = new y(); // <- TypeError: y is not a constructor
var x = function() { console.log("result is " + my_y; }
module.exports = x;

The reason is that when it is attempting to initialize y, it creates a temporary "y" object (not class, object!) in the dependency system that is somehow not yet a constructor. Then, when x.js is finished being defined, it can continue making y a constructor. Only, x.js has an error in it where it tries to use the non-constructor y.


In my case I was using the prototype name as the object name. For e.g.

function proto1()
{}

var proto1 = new proto1();

It was a silly mistake but might be of help to someone like me ;)


I have a class in one file that I'm importing into a test file:

//Vec.js
class Vec {

}

module.exports.Vec = Vec;

Changing

//Vec.test.js
const Vec = require('./Vec');
const myVec = new Vec(); //TypeError: Vec is not a constructor

to

//Vec.test.js
const {Vec} = require('./Vec');
const myVec = new Vec(); //Succeeds!

resolved this error for me.