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

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 {} ???

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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)

Method 2

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;
  }
}

Method 3

I had faced similar issue, after checking all the workaround finally issue got resolved by deleting the node_modules folder and run npm i.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x