【发布时间】:2022-02-16 08:59:36
【问题描述】:
我正在尝试在我的 TypeScript 项目中导入 npm 包 superagent-throttle,但是当我这样做时,我收到了以下消息:
Uncaught TypeError: Class extends value #<Object> is not a constructor or null
如果我使用require,它可以正常工作:
const Throttle = require("superagent-throttle");
但是,我已经开始使用Vite,它不允许使用require,所以我必须改用import。
我引用它的代码想要实例化class 即:
this.throttle = new Throttle({
// set false to pause queue
active: true,
// how many requests can be sent every `ratePer`
rate: 10000,
// number of ms in which `rate` requests may be sent
ratePer: 1000,
// How many requests can be sent concurrently
concurrent: 2
});
我尝试了import 语句的不同变体,但这些都不适合我:
import { Throttle } from "superagent-throttle";
import Throttle from "superagent-throttle";
import * as Throttle from "superagent-throttle";
它们都给出相同的结果 - 它们编译正常,但在运行时失败并显示相同的错误消息(如上)。
superagent-throttle JS 文件中的代码看起来相当晦涩,我不知道它要做什么,所以我正在努力尝试并了解如何解决这个问题:
包文件:node_modules/superagent-throttle/dist/index.js:
'use strict';
var _events = require('events');
var _events2 = _interopRequireDefault(_events);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
...
class Throttle extends _events2.default {
constructor(options) {
super();
// instance properties
this._options({
_requestTimes: [0],
_current: 0,
_buffer: [],
_serials: {},
_timeout: false
});
... etc ...
【问题讨论】:
-
你安装了什么版本的 superagent-throttle?
-
import Throttle from "superagent-throttle"工作正常here。虽然它缺少类型定义(即从未为 Typescript 构建)。也许你最好找到一个开发更积极的库 -
@Phil 我使用的是 1.0.1 版,它是最新的(但在 3 年前发布)。我一直在寻找替代方案,但找不到其他会限制并发请求的东西。我会为 superagent-throttle 编写自己的 TypeScript 类型文件 - 但我不知道如何为具有如此复杂界面的库执行此操作
标签: javascript typescript vite superagent