Reputation: 15903
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:
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;
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?
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 {} ???
Upvotes: 26
Views: 80059
Reputation: 1
I had faced similar issue, after checking all the workaround finally issue got resolved by deleting the node_modules folder and run npm i
.
Upvotes: -1
Reputation: 6160
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;
}
}
Upvotes: 6
Reputation: 15903
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)
Upvotes: 53