Class constructor cannot be invoked without 'new' - typescript with commonjs

For those who are using ts-node, it could be possible that your tsconfig.json is unable to be loaded by ts-node.

  1. Make sure you've set the below option for tsconfig.json:
{
    "compilerOptions": {
        "target": "ES6",
        ...
    }
}
  1. Try ts-node --script-mode or use --project to specify the path of your tsconfig.json.

TypeScript transpiles a class to its ES5 counterpart, but this way it's necessary that entire class hierarchy is transpiled to ES5.

In case parent class is untranspiled (native class or imported ES6 class, including the ones that were transpiled with Babel), this won't work, because TypeScript relies on var instance = Parent.call(this, ...) || this trick to call parent constructor, while ES6 classes should be called only with new.

This problem should be solved in Node.js by setting TypeScript target option to es6 or higher. Modern Node.js versions support ES6 classes, there is no need to transpile them.

The same problem applies to Babel.


I came across the same problem using javascript, webpack, and babel (but no TypeScript).

I found a solution based on ES6/Babel Class constructor cannot be invoked without 'new'

I needed to explicitly include colyseus in my babel loader. Below is the snippet in my webpack config file:

  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        include: /colyseus/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  },