You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发Twitch连接器时遇EventEmitter构造器TypeError的修复方法

Twitch连接器报错:TypeError: Class constructor EventEmitter cannot be invoked without 'new'

错误信息

BasicPubSubClient.js:25
        var _this = _super.call(this) || this;
                           ^

TypeError: Class constructor EventEmitter cannot be invoked without 'new'

导入代码

import {PubSubClient} from 'twitch-pubsub-client';
import {ApiClient} from 'twitch';
import {StaticAuthProvider} from 'twitch-auth';

调用代码

class pubSubClientHandler {
    constructor() {
        this.pubSubClient = new PubSubClient(); // 报错位置
        this.authProvider = new StaticAuthProvider(process.env.TWITCH_CLIENT_ID, process.env.TWITCH_ACCESS_TOKEN);
        this.apiClient = new ApiClient({ authProvider: this.authProvider });
    }
}

package.json配置

{
  "name": "starter-express-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files",
    "start": "npm run build && node dist/index.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/cyclic-software/starter-express-api.git"
  },
  "author": "Pedro Azevedo",
  "license": "ISC",
  "type": "commonjs",
  "bugs": {
    "url": "https://github.com/cyclic-software/starter-express-api/issues"
  },
  "homepage": "https://github.com/cyclic-software/starter-express-api#readme",
  "dependencies": {
    "@babel/runtime": "^7.23.8",
    "core-js": "^3.35.1",
    "cron": "^3.1.6",
    "ejs": "^3.1.9",
    "express": "^4.18.2",
    "express-ws": "^5.0.2",
    "openai": "^4.20.1",
    "promisify": "^0.0.3",
    "regenerator-runtime": "^0.14.1",
    "request": "^2.88.2",
    "say": "^0.16.0",
    "sound-play": "^1.1.0",
    "tmi.js": "^1.8.5",
    "twitch": "^4.6.7",
    "twitch-auth": "^4.6.7",
    "twitch-pubsub-client": "^4.6.7",
    "ws": "^8.14.2"
  },
  "engines": {
    "node": ">=14.0.0"
  },
  "devDependencies": {
    "@babel/cli": "^7.23.4",
    "@babel/core": "^7.23.7",
    "@babel/plugin-proposal-class-properties": "^7.18.6",
    "@babel/plugin-transform-runtime": "^7.23.7",
    "@babel/preset-env": "^7.23.8",
    "babel-loader": "^9.1.3",
    "rimraf": "^5.0.5"
  }
}

.babelrc配置

{
  "presets": [
    [
      "@babel/preset-env",
      {"targets": {"esmodules": true}}
    ]
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties"
  ]
}

修复方案

这个错误核心是Babel转译类继承时的兼容性问题:twitch-pubsub-client内部类继承Node.js的EventEmitter,但转译输出试图用非new方式调用类构造函数,导致报错。可按以下步骤修复:

1. 调整Babel配置,完善polyfill处理

修改.babelrc,让Babel正确处理类继承转译,自动引入必要polyfill:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {"esmodules": true},
        "useBuiltIns": "usage",
        "corejs": 3
      }
    ]
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-transform-runtime"
  ]
}

2. 解决模块类型冲突

你的package.json设置了"type": "commonjs",但代码用了ES模块的import语法,可能引发加载兼容问题,二选一调整:

  • 保留CommonJS:将import改为require:
    const {PubSubClient} = require('twitch-pubsub-client');
    const {ApiClient} = require('twitch');
    const {StaticAuthProvider} = require('twitch-auth');
    
  • 改用ES模块:把"type": "commonjs"改为"type": "module",确保Babel转译输出适配ES模块规范。

3. 升级twitch相关依赖(可选)

当前使用的twitch系列依赖为4.6.7旧版本,新版本可能已修复类继承兼容问题,可尝试升级到最新稳定版:

npm update twitch twitch-auth twitch-pubsub-client

4. 升级Node.js版本

虽然配置要求>=14.0.0,但建议使用Node.js 16+版本,对ES类的原生支持更完善,能减少转译带来的问题。


内容的提问来源于stack exchange,提问作者Pedro Azevedo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 22:05:20