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

使用pptxgenjs遇TypeScript构造报错,求解决方法

解决PptxGenJS在TypeScript中无法实例化的问题

我是NodeJS新手,尝试在NodeJS应用的src/index.ts文件中编写如下代码:

import pptxgenjs from 'pptxgenjs';
const pres = new pptxgenjs();

(此写法来自PptxGenJS官方文档示例)

但TypeScript编译器报错:

This expression is not constructable.
Type 'typeof import("########/node_modules/pptxgenjs/types/index")' has no construct signatures.

我的tsconfig.json配置如下:

{
    "compilerOptions": {
        "module": "NodeNext",
        "moduleResolution": "NodeNext",
        "target": "ES2020",
        "sourceMap": true,
        "outDir": "dist",
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true
    },
    "include": ["src/**/*"]
}

解决方案:

  • 调整导入方式:PptxGenJS的TypeScript类型定义中,默认导出并非构造函数,需改用命名导入PptxGenJS类:
import { PptxGenJS } from 'pptxgenjs';
const pres = new PptxGenJS();
  • 更新依赖版本:旧版本的PptxGenJS可能存在类型定义缺陷,执行命令更新到最新版:
npm install pptxgenjs@latest
  • 适配模块解析规则:若上述方法无效,可修改tsconfig.json的模块配置,将module和moduleResolution改为Node16或CommonJS:
{
    "compilerOptions": {
        "module": "CommonJS",
        "moduleResolution": "Node",
        "target": "ES2020",
        "sourceMap": true,
        "outDir": "dist",
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true
    },
    "include": ["src/**/*"]
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 23:59:55