Class constructor cannot be invoked without 'new' for TSdx package

1.9k views Asked by At

I created a npm package using TSdx for a small Jest reporter. If I use that package in another project, I get the following error.

Uncaught TypeError: Class constructor BaseReporter cannot be invoked without 'new'
    at new ExampleReporter (..\dist\example-reporter.cjs.development.js:37:26)

The ExampleReporter inherits from the BaseReporter class provided by Jest.

import BaseReporter from '@jest/reporters/build/base_reporter';

export class ExampleReporter extends BaseReporter {
  // ...
}

As mentioned in Class constructor cannot be invoked without 'new', it can be fixed by setting up Babel. But I couldn't find a working solution with the TSdx documentation.

How can I configure the TSdx build?


You can reproduce the issue using the watch mode.

$ npx tsdx watch --onSuccess node .
...
  Watching for changes

Welcome to Node.js v12.18.2.
Type ".help" for more information.
> new (require('.').ExampleReporter)()
Uncaught TypeError: Class constructor BaseReporter cannot be invoked without 'new'
1

There are 1 answers

0
sschmeck On BEST ANSWER

You can just set --target node to fix the issue (since the default value is browser).

$ npx tsdx build --target node

Alternatively you can add a Babel configuration file .babelrc to your TDdx project and setup your target environment.

{
  "presets": [
    ["@babel/preset-env", { "targets": { "esmodules": true } }]
  ]
}