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

使用lottie-compress报错:TypeError: LottieCompress不是构造函数

解决lottie-compress报错:TypeError: LottieCompress is not a constructor

我尝试用lottie-compress压缩Lottie资源,环境为Node.js v18.13.0,package.json已设置type: "module"。按官方文档提供的代码运行时触发如下错误:

TypeError: LottieCompress is not a constructor
    at compressLottie (file:///Users/jeffleary/Documents/Coding.nosync/testing/lottie-compress/index.js:7:26)
    at file:///Users/jeffleary/Documents/Coding.nosync/testing/lottie-compress/index.js:12:1
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

我的代码:

import LottieCompress from "lottie-compress";
import data from "./flowA.json" assert { type: "json" };

async function compressLottie() {
  const lottieCompress = new LottieCompress(data);
  const ret = await lottieCompress.execute();
  assert(ret.tiny === "75");
}
compressLottie();

package.json配置:

{
  "type": "module",
  "dependencies": {
    "lottie-compress": "^1.1.4"
  }
}

解决方法

问题核心是导入方式错误:lottie-compress的ESM导出为命名导出,而非默认导出。同时原代码还缺少对Node.js内置assert模块的导入,一并修正:

// 改为命名导入LottieCompress
import { LottieCompress } from "lottie-compress";
import data from "./flowA.json" assert { type: "json" };
// 显式导入内置assert模块
import assert from "node:assert";

async function compressLottie() {
  const lottieCompress = new LottieCompress(data);
  const ret = await lottieCompress.execute();
  assert(ret.tiny === "75");
}
compressLottie();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 03:24:27