Skip to content
Advertisement

Javascript ES6 TypeError: Class constructor Client cannot be invoked without ‘new’

I have a class written in Javascript ES6. When I try to execute nodemon command I always see this error TypeError: Class constructor Client cannot be invoked without 'new'

The full error is mentioned below:

JavaScript

What I am trying to do is, I have created a class and then created an instance of that class. Then I am trying to export that variable.

The class structure is defined below:

JavaScript

How I am trying to export the variable ->

JavaScript

You can find the full code here –> Link. Code generated by Babel –> Link.

Advertisement

Answer

The problem is that the class extends native ES6 class and is transpiled to ES5 with Babel. Transpiled classes cannot extend native classes, at least without additional measures.

JavaScript

results in something like

JavaScript

Since ES6 classes should be only called with new, NativeBar.call results in error.

ES6 classes are supported in any recent Node version, they shouldn’t be transpiled. es2015 should be excluded from Babel configuration, it’s preferable to use env preset set to node target.

The same problem applies to TypeScript. The compiler should be properly configured to not transpile classes in order for them to inherit from native or Babel classes.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement