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 {} ???
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.
To use the shortcut just right click on the project / . js file → Run/Debug As → Node. js Application.
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.
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.
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)
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; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With