我有一个用Javascript ES6编写的类。当我尝试执行nodemon
命令时,我总是看到此错误TypeError: Class constructor Client cannot be invoked without 'new'
完整错误如下所述:
/Users/akshaysood/Blockchain/fabricSDK/dist/application/Transaction.js:45
return (0, _possibleConstructorReturn3.default)(this, (FBClient.__proto__ || (0, _getPrototypeOf2.default)(FBClient)).call(this, props));
^
TypeError: Class constructor Client cannot be invoked without 'new'
at new FBClient (/Users/akshaysood/Blockchain/fabricSDK/dist/application/Transaction.js:45:127)
at Object.<anonymous> (/Users/akshaysood/Blockchain/fabricSDK/dist/application/Transaction.js:195:14)
at Module._compile (module.js:641:30)
at Object.Module._extensions..js (module.js:652:10)
at Module.load (module.js:560:32)
at tryModuleLoad (module.js:503:12)
at Function.Module._load (module.js:495:3)
at Module.require (module.js:585:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/akshaysood/Blockchain/fabricSDK/dist/routes/users.js:11:20)
我想做的是,我创建了一个类,然后创建了该类的实例。然后,我尝试导出该变量。
类结构定义如下:
class FBClient extends FabricClient{
constructor(props){
super(props);
}
<<< FUNCTIONS >>>
}
我如何尝试导出变量->
var client = new FBClient();
client.loadFromConfig(config);
export default client = client;
您可以在此处找到完整的代码> https://hastebin.com/kecacenita.js Babel> https://hastebin.com/fabewecumo.js
生成的代码答案 0 :(得分:12)
问题是该类扩展了本机ES6类,并通过Babel转换为ES5。转译的类至少在没有其他措施的情况下不能扩展本机类。
class TranspiledFoo extends NativeBar {
constructor() {
super();
}
}
结果类似
function TranspiledFoo() {
var _this = NativeBar.call(this);
return _this;
}
// prototypically inherit from NativeBar
由于仅应使用new
来调用ES6类,所以NativeBar.call
会导致错误。
ES6类在任何最新的Node版本中均受支持,不应进行编译。 es2015
应该从Babel配置中排除,最好使用env
preset set to node
target。
答案 1 :(得分:8)
我不是在编译Javascript,而是在编译Typescript,却遇到了同样的问题。
我编辑了Typescript编译器配置文件parser = etree.XmlParser()
when(etree.XMLParser(any()).thenReturn(parser)
,以生成ES2017 Javascript代码:
tsconfig.json
ES2015而不是默认值是什么? —然后一切正常。
(也许这个答案可能对使用Typescript的人很有帮助,就像我一样,当他们搜索相同的错误消息时找到此问题。)
答案 2 :(得分:6)
您可以在package.json
中使用目标配置。
将@babel/preset-env
设置为“ true”。
下面是我在文件中使用示例:
esmodules
答案 3 :(得分:1)
对于正在使用ts-node的用户来说,ts-node可能无法加载您的tsconfig.json
。
首先,请确保您为tsconfig.json
设置了以下选项:
{
"compilerOptions": {
"target": "ES6",
...
}
}
然后,您可以尝试ts-node --script-mode
或使用--project
指定到tsconfig.json
的路径。
答案 4 :(得分:1)
对于 Angular 9+,这可能也是使用 typescript 编译标志的问题:
downlevelIteration
importHelpers
更多信息在这里L https://www.typescriptlang.org/tsconfig#downlevelIteration。
尝试在您的 tsconfig.josn
中使用此标志:
{
"compilerOptions": {
...
"downlevelIteration": true,
"importHelpers": true,
...
}
}
如果没有 "importHelpers": true
,它也应该可以工作。
此解决方案主要用于 Angular 9+ 和 ngcc 编译器 - 这是 Angular 11 发生的问题。自此维护包的自定义配置(自定义 ng-packgr 配置 - 在 Angular 6 中引入了 Angular CLI 库生成)项目中有 Angular 4+。将这些包迁移到 Angular 9+ 库(项目文件夹)后,错误开始发生。我发现这些标志(严格来说是 downlevelIteration)正好解决了这个问题。
答案 5 :(得分:0)
希望您已经能够解决问题,这是我针对此错误的解决方案:
Blockquote
class indexRoutes
{
public router: Router= Router();
}
const INDEX_ROUTES = new indexRoutes();
export default INDEX_ROUTES.router;
答案 6 :(得分:0)
如果您不使用babel和简单的打字稿。我在材料上犯了错误。尝试减少版本并将其降低到相似的版本。我的软件包有多种版本。我通过降低cdk,material等软件包的软件包版本来解决了这个问题。
答案 7 :(得分:0)
就我而言,它是来自“mobx-react-lite”的观察者。
答案 8 :(得分:-3)
解决方案是降级软件包版本。
npm uninstall connect-mongo
npm i connect-mongo@3