Search code examples
compressionlottie

LottieCompressor Library


I am trying to use lottie-compress to compress a lottie asset.

I am using node v18.13.0 and have specified type module in the package.json.

I am getting the following error trying to run the code supplied in the documentation:

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)

Can someone help me get this thing working please? Thank you!

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

    //console.log(data);

    async function compressLottie() {
      const lottieCompress = new LottieCompress(data);
      const ret = await lottieCompress.execute();
      assert(ret.tiny === "75");
    }
    compressLottie();
{
  "type": "module",
  "dependencies": {
    "lottie-compress": "^1.1.4"
  }
}


Solution

  • So after some screwing around, I finally figured that since you're using Import,

    you need to add "default" to LottieCompress for it to work, like so

    const lottieCompress = new LottieCompress.default(data);
    

    Note that "ret" is an object from the compressed data of the original json file, you can then write the value of "ret" into a new file after stringifying it.

    and the compression ratio is quite low though

    Good luck