【问题标题】:TypeError: Class constructor Client cannot be invoked without 'new'TypeError:没有'new'就不能调用类构造函数客户端
【发布时间】:2021-06-08 12:10:36
【问题描述】:

我正在开发一个使用 ts-node(在脚本模式下)、typescript 和 discord.js 的 discord 机器人框架。测试核心客户端类时出现此错误:


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)

围绕 StackOverflow 有很多相同问题的答案,大多数建议将 tsconfig 中的“目标”值设置为“es6”或更高。我的问题是该解决方案对我不起作用。

这是我的代码:

// src/struct/Client.ts

import { Client, ClientOptions, Snowflake } from 'discord.js';
import * as discord from 'discord.js';

interface DigsClientOptions {
    ownerID?: Snowflake | Array<Snowflake>;
}

class DigsClient extends Client {
    public constructor(
        options?: DigsClientOptions,
        discordOptions?: ClientOptions
    ) {
        super();

        this.ownerID = options.ownerID;

        return this;
    }

    ownerID: Snowflake | Array<Snowflake>;
}

export { DigsClient, DigsClientOptions };

// test/index.ts

import { DigsClient } from '../src/index';
import { config } from 'dotenv';
config();
const token = process.env.TOKEN;

const client = new DigsClient({ ownerID: '585813838490894346' });

client.login(token);

//.tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "module": "es6"
    },
    "exclude": ["node_modules"]
}


使用ts-nodets-node-script 时会出现此错误,我知道它会检测到 tsconfig,因为我没有收到使用 es6 模块的错误。

希望任何人都可以提供帮助!

【问题讨论】:

  • 尝试将module 更改为commonjstarget 是您要使用的语法,module 是您在编译时要使用的语法。使用commonjs,因为NodeJS上通常使用的是CommonJS。
  • @nouvist 看起来问题现在已经解决了,这确实是 ts-node 和 typescript 的编译器选项非常混乱的问题。不过,您的回答有效,谢谢!

标签: javascript node.js typescript ts-node


【解决方案1】:

由于NodeJS遵循CommonJS模块系统,我认为原因是你应该将module设置为CommonJS。您可以将target 保留到ES6,只要它不中断。因为 AFAIK、Node 和现代浏览器支持 ES6 语法。

但是,targetmodule 之间有什么区别。 target 是您针对的整个语法,而 module 仅适用于您如何导入其他模块。 NodeJS 仅支持 CommonJS 直到 14.x 版本。所以,推荐在 NodeJS Typescript 项目上使用CommonJS


就像我说的,它只是如何导入其他模块。 CommonJS 是一个模块系统,它是一个允许您导入其他模块的系统,例如 html 上的&lt;script&gt; 标签。这是 CommonJS 示例。

const foobar = require('./foobar.js');
foobar();

NodeJS 使用这个模块系统,因为 Javascript 还没有。

然后,EcmaScript(Javascript 标准)引入了 ESModule,它是 CommonJS 的标准化替代方案。 ESModule 是 EcmaScript 标准语法的一部分。它的语法是这样的。

import foobar from './foobar.js';
foobar();

所以,您使用的是 ESModule,但 NodeJS 只了解 CommonJS。所以我们需要把ES6编译成CommonJS,这样NodeJS才能理解。


但现在,NodeJS 在 13.x 或 14.x 上支持 ESModule(我不完全确定)。但 NodeJS 仍将默认使用 CommonJS。所以,如果你想使用它,你需要告诉 NodeJS 你的包正在使用 ESModule。

有多种选择。首先,使用.mjs 扩展名,其次是在package.json 上将其更改为默认值。

{
  "name": "my-epic-package",
  "version": "1.0.0",
  "type": "module", // here
  ...
}

因此,启用类型模块后,您可以保留tsconfig.json 以使用module

【讨论】:

    猜你喜欢
    • 2019-01-22
    • 2018-11-12
    • 2019-04-21
    • 2021-05-17
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多