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

Chrome扩展脚本引入tiny-timer模块报非构造函数错误如何解决

问题解决方法

报错原因

从你提供的控制台输出可以看到,require("tiny-timer")返回的是完整的ES模块对象,其中default属性才是你需要调用的Timer构造类,你直接对整个模块对象执行实例化操作,自然会报「不是构造函数」的错误。

解决方案,任选其一即可:

方案1:修改CommonJS导入写法

给require的结果加上.default获取默认导出的类:

import "../css/popup.css";
// 增加.default取默认导出的Timer类
const Timer = require("tiny-timer").default;
console.log(Timer);
const timer = new Timer();

方案2:改用ES6 import语法导入

直接用import语法获取默认导出,更符合ES模块的使用规范:

import "../css/popup.css";
// 直接导入默认导出的Timer类
import Timer from "tiny-timer";
console.log(Timer);
const timer = new Timer();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 03:57:04