close

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

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Javascript ES6 TypeError: Class constructor Client cannot be invoked without ‘new’ in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How To Solve Javascript ES6 TypeError: Class constructor Client cannot be invoked without ‘new’ Error ?

  1. How To Solve Javascript ES6 TypeError: Class constructor Client cannot be invoked without 'new' Error?

    To Solve Javascript ES6 TypeError: Class constructor Client cannot be invoked without 'new' Error Here The problem is that the class extends the native ES6 class and is transpiled to ES5 with Babel. Transpiled classes cannot extend native classes, at least without additional measures.

  2. Javascript ES6 TypeError: Class constructor Client cannot be invoked without 'new'

    To Solve Javascript ES6 TypeError: Class constructor Client cannot be invoked without 'new' Error Here The problem is that the class extends the native ES6 class and is transpiled to ES5 with Babel. Transpiled classes cannot extend native classes, at least without additional measures.

Solution 1

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

class TranspiledFoo extends NativeBar {
  constructor() {
    super();
  }
}

results in something like

function TranspiledFoo() {
  var _this = NativeBar.call(this) || this;
  return _this;
}
// prototypically inherit from NativeBar 

Since ES6 classes should be only called with newNativeBar.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.

Solution 2

I was transpiling not Javascript but Typescript and ran into the same problem.

I edited the Typescript compiler config file, tsconfig.jsonto generate ES2017 Javascript code:

{
    "compilerOptions": {
        "target": "ES2017",

instead of whatever the default is, ES2015? — then all worked fine.

(Maybe this answer can be helpful for people who use Typescript and find this question when they search for the same error message like I did.)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment