我使用TSdx为一个小型取笑记者创建了一个npm包。如果在另一个项目中使用该包,则会得到以下错误。
Uncaught TypeError: Class constructor BaseReporter cannot be invoked without 'new'
at new ExampleReporter (..\dist\example-reporter.cjs.development.js:37:26)ExampleReporter继承自Jest提供的BaseReporter类。
import BaseReporter from '@jest/reporters/build/base_reporter';
export class ExampleReporter extends BaseReporter {
// ...
}正如在没有“new”就不能调用类构造函数中提到的,它可以通过设置Babel来修复。但是我找不到TSdx文档的有效解决方案。
如何配置TSdx build
您可以使用监视模式再现问题。
$ 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'发布于 2020-09-29 08:56:47
您可以将--target node设置为修复问题(因为默认值是browser)。
$ npx tsdx build --target node或者,您可以将Babel配置文件.babelrc添加到TDdx项目中,并设置目标环境。
{
"presets": [
["@babel/preset-env", { "targets": { "esmodules": true } }]
]
}https://stackoverflow.com/questions/64099319
复制相似问题