Javascript

TypeError Class extends value undefined is not a function or null

19 September 2026 · 12 min read

TypeError Class extends value undefined is not a function or null

Encountering the dreaded “TypeError: Class extends value undefined is not a function or null” in JavaScript can be a frustrating experience, especially when you’re deep in development. This cryptic error message often arises when attempting to create a class that inherits from another, but the JavaScript interpreter can’t find or properly interpret the parent class. It typically indicates a problem with how you’re importing, defining, or referencing the class you’re trying to extend. Understanding the root causes of this error and how to troubleshoot it is crucial for any JavaScript developer, whether you are working on a small personal project or a large-scale enterprise application. Let’s dive into the common culprits behind this error and learn how to resolve them, so you can get back to building amazing things.

Understanding the TypeError

The “TypeError: Class extends value undefined is not a function or null” error specifically points to an issue during class inheritance. In JavaScript, the extends keyword is used to create a subclass (child class) that inherits properties and methods from a superclass (parent class). If the value after extends is not a valid constructor function or null, JavaScript throws this error. This usually happens when the parent class you’re trying to extend is undefined or hasn’t been properly imported or defined before the child class attempts to use it. Debugging this error requires carefully examining your import statements, class definitions, and the order in which your code is executed.

One common scenario is when the module containing the parent class hasn’t been fully loaded or executed before the child class is defined. JavaScript executes code sequentially, so if the parent class definition comes later in the file or in a separate file that hasn’t been imported yet, the child class will attempt to extend an undefined value. Another potential cause is a typo in the name of the parent class or in the import statement. Even a small mistake can prevent JavaScript from finding the correct class definition. According to a Stack Overflow survey, import/export issues contribute to a significant percentage of JavaScript errors, highlighting the importance of careful attention to detail in module management [Stack Overflow Developer Survey 2021].

To effectively troubleshoot this error, it’s helpful to understand the core principles of class inheritance in JavaScript. When you use extends, JavaScript creates a prototype chain that links the child class to the parent class. This allows the child class to inherit properties and methods from the parent. However, this chain can only be established if the parent class is a valid constructor function. If the parent class is undefined, null, or any other non-function value, the prototype chain cannot be created, leading to the TypeError. It is also good to note that even if the parent class is defined, issues with circular dependencies could also lead to this error.

Common Causes and Solutions

Let’s explore some of the most frequent causes of the “TypeError: Class extends value undefined is not a function or null” error and how to fix them:

  • Incorrect Import Statements: This is perhaps the most common culprit. Double-check your import statements to ensure you’re importing the parent class correctly, specifying the correct path, and using the correct syntax (e.g., import ParentClass from ‘./ParentClass’;).
  • Circular Dependencies: If two or more modules depend on each other, it can create a circular dependency, where each module tries to import the other before it’s fully defined. This can lead to one or both classes being undefined when the extends keyword is used.
  • Typographical Errors: A simple typo in the class name or import path can prevent JavaScript from finding the parent class. Carefully review your code for any spelling mistakes.
  • Scope Issues: If the parent class is defined within a limited scope (e.g., inside a function), it may not be accessible from the child class. Ensure the parent class is defined in a scope that’s accessible to the child class.

For example, consider the following scenario where you have two files: ParentClass.js and ChildClass.js. If ChildClass.js attempts to import ParentClass.js before ParentClass.js has fully executed, you might encounter this error. The solution is to ensure that ParentClass.js is fully executed before ChildClass.js tries to import it. This can often be achieved by adjusting the order of your import statements or using dynamic imports.

Here’s another common pitfall: exporting a class as the default export but importing it using named imports, or vice-versa. For instance, if ParentClass.js exports the class as export default ParentClass;, you should import it in ChildClass.js as import ParentClass from ‘./ParentClass’;. Conversely, if ParentClass.js exports the class as export class ParentClass;, you should import it in ChildClass.js as import { ParentClass } from ‘./ParentClass’;. Mixing these up can lead to the “undefined” value and the subsequent TypeError. According to MDN Web Docs, understanding the difference between default and named exports is essential for avoiding import-related errors [MDN Web Docs - Export].

Debugging Strategies

When faced with the “TypeError: Class extends value undefined is not a function or null”, systematic debugging is key. Here are some strategies to help you pinpoint the root cause:

  1. Use console.log(): Add console.log() statements to your code to check the value of the parent class at various points. This can help you determine whether the parent class is actually defined and what its value is.
  2. Check Import Order: Make sure that the parent class is imported before the child class. If necessary, adjust the order of your import statements.
  3. Examine Circular Dependencies: Use a dependency analysis tool to identify any circular dependencies in your project. Tools like madge can help you visualize your module dependencies and spot potential circular references.
  4. Use a Debugger: Use a debugger (e.g., the one built into your browser or your IDE) to step through your code and inspect the values of variables at each step. This can help you identify exactly where the parent class becomes undefined.

For instance, if you suspect an import issue, add console.log(ParentClass) immediately after the import statement in ChildClass.js. If the output is “undefined”, then you know that the import is not working correctly. Another useful technique is to temporarily comment out the extends keyword and the code that relies on the inherited properties and methods. This can help you isolate the issue to the inheritance mechanism. By systematically applying these debugging strategies, you can narrow down the source of the error and implement the appropriate fix.

Remember, the key to successful debugging is to break down the problem into smaller, manageable parts. Don’t try to fix everything at once. Instead, focus on one potential cause at a time and use debugging tools and techniques to verify your assumptions. And always, always double-check your spelling!

Preventing the Error in the Future

While fixing the “TypeError: Class extends value undefined is not a function or null” is important, preventing it from happening in the first place is even better. Here are some best practices to help you avoid this error in your future projects:

  • Use a Module Bundler: Tools like Webpack, Parcel, or Rollup can help you manage your module dependencies and ensure that modules are loaded in the correct order.
  • Follow a Consistent Import/Export Style: Choose a consistent style for importing and exporting modules (e.g., always use default exports or always use named exports) and stick to it throughout your project.
  • Avoid Circular Dependencies: Design your modules to minimize dependencies on each other. If circular dependencies are unavoidable, consider refactoring your code to break the cycle.
  • Write Unit Tests: Write unit tests for your classes to ensure that they are defined correctly and that inheritance is working as expected.

By following these best practices, you can significantly reduce the likelihood of encountering the “TypeError: Class extends value undefined is not a function or null” error in your JavaScript projects. Investing time in establishing a solid foundation for your codebase and adopting a disciplined approach to module management will pay dividends in the long run by reducing debugging time and improving the overall quality of your code. Consider using a linter like ESLint with configured import/export rules to catch potential issues early in the development process. These tools can automatically enforce coding standards and identify potential problems before they lead to runtime errors.

Here’s a featured snippet-optimized paragraph: To resolve the “TypeError: Class extends value undefined is not a function or null”, focus on verifying import statements, ensuring correct paths, and addressing any circular dependencies. This error arises when a class attempts to inherit from a parent class that is either undefined, not a function, or null, commonly due to import issues or circular references in your code. By methodically checking these aspects, you can effectively troubleshoot and fix the underlying problem, restoring proper class inheritance in your JavaScript application.

Infographic here
FAQ ---
Q: What does "undefined is not a function or null" mean?
A: This means that JavaScript is expecting a function or a value that can be treated as a function (like a class constructor), but it's finding an undefined value or a null value instead. This often happens when you try to call a method on an object that doesn't exist or has been set to undefined.
Q: How do I check if a class is defined?
A: You can use typeof ClassName !== 'undefined' to check if a class is defined. If the class is defined, this expression will evaluate to true. Otherwise, it will evaluate to false.
Q: Can this error occur with functional components in React?
A: While this specific error relates to class inheritance, similar issues can arise with React functional components if you're trying to use a component that hasn't been properly imported or defined. The debugging strategies are similar: check your import statements and ensure that the component is defined in the correct scope.
We've covered the common causes, debugging strategies, and preventative measures for the "TypeError: Class extends value undefined is not a function or null." Remember, careful attention to detail in your import statements, module dependencies, and code structure is paramount. By adopting these practices, you'll be well-equipped to tackle this error and build more robust JavaScript applications. Need more help with debugging? [Check out our advanced JavaScript debugging guide](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c).

Now that you’re armed with the knowledge to diagnose and resolve this tricky TypeError, take some time to review your own projects and identify any potential vulnerabilities. Perhaps explore refactoring your code to eliminate circular dependencies or implement more robust unit testing. The goal is to create a more resilient and maintainable codebase. If you found this guide helpful, share it with your fellow developers and let’s continue to build a stronger JavaScript community together. And if you’re still struggling, consider reaching out to online forums or communities for personalized assistance. After all, we all learn from each other!

Question & Answer :
I am getting the following error when trying to create these entities.

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

I am assuming this has something to do with circular dependencies, but how is that supposed to be avoided when using table inheritance and one to many relationships?

It is complaining about the following javascript at BaseComic_1.BaseComic.

let Variant = class Variant extends BaseComic_1.BaseComic {

Here is the complete file.

"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); const typeorm_1 = require("typeorm"); const Comic_1 = require("./Comic"); const BaseComic_1 = require("./BaseComic"); let Variant = class Variant extends BaseComic_1.BaseComic { }; __decorate([ typeorm_1.ManyToOne(type => Comic_1.Comic, comic => comic.variants), __metadata("design:type", Comic_1.Comic) ], Variant.prototype, "comic", void 0); Variant = __decorate([ typeorm_1.ClassEntityChild() ], Variant); exports.Variant = Variant; //# sourceMappingURL=Variant.js.map 

import {Entity, Column, PrimaryGeneratedColumn, OneToMany} from "typeorm"; import {Comic} from "./Comic"; @Entity() export class Series { @PrimaryGeneratedColumn() public id: number; @Column("text", { length: 30 }) public copyright: string; @Column("text", { length: 100 }) public attributionText: string; @Column("text", { length: 150 }) public attributionHTML: string; @Column("text", { length: 50 }) public etag: string; @Column("text", { length: 200 }) public title: string; @Column("text") public description: string; @Column("number", { length: 4 }) public startYear: number; @Column("number", { length: 4 }) public endYear: number; @Column("text", { length: 20 }) public rating: string; @Column("text", { length: 20 }) public type: string; @Column("text") public thumbnail: string; @OneToMany(type => Comic, comic => comic.series) public comics: Array<Comic>; } 

import {Entity, TableInheritance, PrimaryGeneratedColumn, Column, ManyToOne, DiscriminatorColumn} from "typeorm"; import {Series} from "./Series"; @Entity() @TableInheritance("class-table") @DiscriminatorColumn({ name: "type", type: "string"}) export class BaseComic { @PrimaryGeneratedColumn() public id: number; @Column("text", { length: 30 }) public copyright: string; @Column("text", { length: 100 }) public attributionText: string; @Column("text", { length: 150 }) public attributionHTML: string; @Column("text", { length: 50 }) public etag: string; @Column("text", { length: 200 }) public title: string; @Column("int") public issue: number; @Column("text") public variantDescription: string; @Column("boolean") public variant: boolean; @Column("text") public description: string; @Column("int") public pageCount: number; @Column("date") public onSaleDate: Date; @Column("date") public unlimitedDate: Date; @Column("text") public thumbnail: string; @ManyToOne(type => Series, series => series.comics) public series: Series; } 

import {OneToMany, ClassEntityChild} from "typeorm"; import {Variant} from "./Variant"; import {BaseComic} from "./BaseComic"; @ClassEntityChild() export class Comic extends BaseComic { @OneToMany(type => Variant, variant => variant.comic) public variants: Variant[]; } 

import {ManyToOne, ClassEntityChild} from "typeorm"; import {Comic} from "./Comic"; import {BaseComic} from "./BaseComic"; @ClassEntityChild() export class Variant extends BaseComic { @ManyToOne(type => Comic, comic => comic.variants) public comic: Comic; } 

I was having the same issue. It turns out I was circularly importing classes, which is apparently a limitation. (See these GitHub issues: #20361, #4149, #10712)

Note that it seems that the circular reference is also limited between files, not simply types.

See this other answer