Javascript TypeError:没有';类构造函数客户端无法调用;新';
我正在开发一个discord机器人框架,它使用ts节点(脚本模式)、typescript和discord.js。在测试核心客户端类时,我遇到以下错误:Javascript TypeError:没有';类构造函数客户端无法调用;新';,javascript,node.js,typescript,ts-node,Javascript,Node.js,Typescript,Ts Node,我正在开发一个discord机器人框架,它使用ts节点(脚本模式)、typescript和discord.js。在测试核心客户端类时,我遇到以下错误: C:\Users\thedi\Documents\coding\JS-TS\discord-bot\src\struct\Client.ts:13 super() ^ TypeError: Class constructor Client cannot be invoked without 'new' a
C:\Users\thedi\Documents\coding\JS-TS\discord-bot\src\struct\Client.ts:13
super()
^
TypeError: Class constructor Client cannot be invoked without 'new'
at new DigsClient (C:\Users\thedi\Documents\coding\JS-TS\discord-bot\src\struct\Client.ts:13:9)
at Object.<anonymous> (C:\Users\thedi\Documents\coding\JS-TS\discord-bot\test\index.ts:6:16)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module.m._compile (C:\Users\thedi\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1056:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.require.extensions.<computed> [as .ts] (C:\Users\thedi\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1059:12)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at main (C:\Users\thedi\AppData\Roaming\npm\node_modules\ts-node\src\bin.ts:198:14)
/.tsconfig.json
{
“编译器选项”:{
“目标”:“es6”,
“模块”:“es6”
},
“排除”:[“节点_模块”]
}
使用ts node
和ts node script
时会发生此错误,我知道它会检测到tsconfig,因为我没有收到使用es6模块的错误
希望任何人都能帮忙 上的
目标
表示“要使用什么语法”,而模块
表示“编译时需要什么语法”
由于NodeJS遵循CommonJS模块系统,我认为原因是您应该将module
设置为CommonJS
。您可以保留目标
,因为使用ES6
将为您带来最新的JavaScript功能
如果你想知道CommonJS是什么。它是一个模块系统,允许您导入其他模块,如html上的标签。下面是一个例子
const foobar=require('./foobar.js');
foobar();
nodej使用这个模块系统,因为Javascript还没有
但是,EcmaScript(Javascript标准)引入了ESModule,它是CommonJS的标准化替代品。ESModule是EcmaScript标准语法(即ES6)的一部分。其语法如下所示
从“/foobar.js”导入foobar;
foobar();
所以,您使用的是ESModule(ES6语法),但NodeJS只理解CommonJS。这就是为什么我们需要将ES6编译成CommonJS,以便NodeJS能够理解
实际上,现在NodeJS已经正式支持ESModule了 Node.js 13.2.0提供了对ECMAScript模块的支持 但是NodeJS将使用CommonJS作为默认值。因此,您需要告诉NodeJS您的包正在使用ESModule 这样做有几种选择。首先使用
.mjs
扩展,然后在package.json
上将其更改为默认值
{
“名称”:“我的史诗套装”,
“版本”:“1.0.0”,
“类型”:“模块”,//此处
...
}
因此,启用类型模块后,您可以保持tsconfig.json
以使用module
es6
您可以访问以查看哪个版本支持哪种语法。尝试将更改为
commonjs
target
是您想要使用的语法,模块
是您在编译时想要使用的语法。使用commonjs
,因为NodeJS上通常使用的是commonjs。@nouvist看起来这个问题现在已经解决了,这确实是一个问题,因为ts node和typescript的编译器选项非常混乱。不过你的回答很有效,谢谢!