Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js v6.2.0 class extends is not a function error?

Tags:

So I'm trying to extend a class in node js and the compiler keeps returning the following error:

TypeError: Class extends value #<Object> is not a function or null 

I checked that I was exporting the class correctly and I am, any ideas? I'll post my code below:

/handler/venue.js:

var VenueViews = require('../views/venue'); // If I remove this the error will dissapear (as expected) class Venue {   constructor(data) {     this.setDataHere = data;   }    main () {    var View = new VenueViews(); // This doesn't run   }   }  module.exports = Venue; 

/views/venue.js:

var Venue = require('../handlers/venue'); console.log  (Venue) // This returns {} ???  class VenueViews extends Venue {   constructor() {     super();   } }  module.exports = VenueViews; 

I know that node supports these es6 features, so I'm unsure why they aren't working?

Edit:

I'm not sure if this is suppose to happen but, when I log my Venue require it returns an empty object {}.

console.log  (Venue) // This returns {} ??? 
like image 673
James111 Avatar asked May 19 '16 00:05

James111


People also ask

Is not a function error JS?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.

How do I start node js in eclipse?

To use the shortcut just right click on the project / . js file → Run/Debug As → Node. js Application.

Does Eclipse support node JS?

js Besides launching and testing code from the IDE using Node. js, additional support is provided for automatically downloading npm packages and exporting N4JS projects to npm. This allows for seamless integration of N4JS projects with existing Node. js based environments.

How does node JS calculate time?

time() method is the console class of Node. js. It is used to starts a timer that is used to compute the time taken by a piece of code or function.


2 Answers

So it turns out I had a circular reference in my code, where I was importing the class that was extending, into the class that itself was extending (tongue twister :P).

The obvious fix was to simply remove the extends reference and find another way of doing what I was trying to achieve. In my case it was passing the Venue class properties down into the VenueViews constructor.

E.g var x = VenueViews(this)

like image 79
James111 Avatar answered Oct 22 '22 16:10

James111


In my instance, it was the same issue as @James111 was experiencing (circular import) due to a factory pattern I was trying to set up in Typescript. My fix was to do move the code into files, similar to the following:

// ./src/interface.ts import { ConcreteClass } from './concrete';  export interface BaseInterface {    someFunction(): any; }  export class Factory {   static build(): BaseInterface {      return new ConcreteClass();   } }  // ./src/base.ts import { BaseInterface } from './interface'; class BaseClass implements BaseInterface {   someFunction(): any {     return true;   } }  // ./src/concrete.ts import { BaseClass } from './base'; export class ConcreteClass extends BaseClass {   someFunction(): any {     return false;   } } 
like image 38
Blaskovicz Avatar answered Oct 22 '22 15:10

Blaskovicz