6

I have 3 files structure in the following order all of which contain 1 class

main.js extends events
events.js extends base
base.js

I've looked into these answers but my problem doesn't appear to look like anything of the following people described. TypeError: Class extends value undefined is not a function or null

main.js

const { Events } = require('./events.js');

module.exports = class Main extends Events {
    constructor(token) {
        super();

        // Some code
    }
}

events.js

const { Base } = require('./base.js');

module.exports = class Events extends Base {
    constructor() {
        super();
    }

    // more code
}

base.js

module.exports = class Base{
    constructor() {
        // Code
    }
}

I'm unable initialize the main class in index.js as this results in the following error:

module.exports = class Events extends Base {
                                      ^

TypeError: Class extends value undefined is not a constructor or null

Am I somehow requiring the classes in a circular way? I'm not sure what I'm missing here.

0

3 Answers 3

10

It is possible you are creating some sort of circular dependency loop by the way you import external modules in index.js.

But, it appears that mostly you just need to change from this:

const { Base } = require('./base.js');

to this:

const Base = require('./base.js');

And from this:

const { Events } = require('./events.js');

to this:

const Events = require('./events.js');

When you do something like this:

const { Base } = require('./base.js');

it's looking for a property on the imported module named Base, but there is no such property. You exported the class as the whole module. So, that's what you need to assign to your variable Base (the whole module):

const Base = require('./base.js');

And the code in index.js that imports main.js will have to be done properly too if it isn't already being done right.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for taking your time to answer, the answer seems really simple but is really helpful as I totally did not notice the difference in how I export stuff.
3

The file reference, line and column number which are reported in the error message turn out to be accurate and useful.

In my case I was extending a class which was imported from '.'. So, it looked like:

import { BaseSchema } from '.';

@ObjectType
export class SomeSchema extends BaseSchema { 
}

That was supposed to work because there was an index.ts file in the folder. However, for some reason it triggered the mentioned error. I managed to fix it, by simply modifying the import statement to a specific path.

import { BaseSchema } from './Base.schema';

I use imports from '.' with index.ts files all the time. So, I do believe that in this specific case there was some kind of circular dependency issue.

In summary:

  • Take a close look to the error message. It probably points to the extends ....
  • Next, look for the import statement of that extended class.
  • Try changing the path of that import statement. That will probably fix it.

Comments

0

When I used Squelize ORM with NodeJS, I got an error that is "Sequelize TypeError: Class extends value undefined is not a constructor or null, NodeJS". I tried to solve this problem and I found some solutions. There's a solution for some people who try to solve this error.

I got this error and solved it using that. If you have the same error, you can use the following solution that I've tried to explain it in code. I hope that it helps you!

module.exports = (sequelize, DataTypes) => {

    // Modeling a table: Inheritance
    class Todo extends sequelize.Model {} // (!) Error

    // if you want to use the above line, add the following line to "lib\sequelize.js file
    // Sequelize.prototype.Model = Model; // add this line here

    class Example extends sequelize.Sequelize.Model { }

    Example.init({
        title: DataTypes.STRING,
        description: DataTypes.STRING,
        status: DataTypes.BOOLEAN
    }, {
        sequelize,
        modelName: 'todo',
        timestamps: true
    });

    return Example;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.