Skip to main content

Installation

HTVG is available in multiple formats to suit your development environment. Choose the installation method that matches your project.

JavaScript/TypeScript via npm

Install HTVG as a dependency in your Node.js, browser, or edge runtime project.
npm install htvg

Browser Usage

The WASM module auto-fetches the .wasm file in browser environments:
import { init, compileDocument } from "htvg";

// Initialize once before any compilation
await init();

const result = compileDocument({
  meta: { width: 400 },
  content: {
    type: "text",
    content: "Hello from the browser!",
    style: { fontSize: 24 }
  }
});

console.log(result.svg);
The init() function must be called once before using any compilation functions. In browsers, it automatically fetches the WASM module.

Node.js Usage

For Node.js, you need to explicitly load the WASM file:
import { init, compileDocument } from "htvg";
import fs from "node:fs";

// Load WASM from node_modules
const wasmBuffer = fs.readFileSync(
  "node_modules/htvg/dist/wasm/htvg_bg.wasm"
);
await init(wasmBuffer);

const result = compileDocument({
  meta: { width: 600 },
  content: {
    type: "flex",
    style: { flexDirection: "column", gap: 12 },
    children: [
      { type: "text", content: "Title", style: { fontSize: 20, fontWeight: 700 } }
    ]
  }
});

console.log(result.svg);

Cloudflare Workers

For Cloudflare Workers and other edge runtimes, import the WASM module directly:
import { init, compileDocument } from "htvg";
import wasmModule from "htvg/htvg.wasm";

await init(wasmModule);

export default {
  async fetch(request) {
    const result = compileDocument({
      meta: { width: 800 },
      content: {
        type: "text",
        content: "Generated at the edge!",
        style: { fontSize: 32 }
      }
    });

    return new Response(result.svg, {
      headers: { "Content-Type": "image/svg+xml" }
    });
  }
};
HTVG’s WASM build is optimized for edge runtimes with minimal overhead and fast cold starts.

Verify Installation

After installation, verify HTVG is working correctly:
import { init, version } from "htvg";

await init();
console.log("HTVG version:", version());

System Requirements

  • Rust: Rust 1.70 or higher
  • Node.js: Node.js 18 or higher (for WASM usage)
  • WASM: WebAssembly support in target environment

Troubleshooting

WASM Module Not Found

If you see “WASM module not found” errors in Node.js:
  1. Ensure you’re passing the WASM buffer to init()
  2. Verify the path to htvg_bg.wasm is correct
  3. Check that the file exists in node_modules/htvg/dist/wasm/

Rust Compilation Errors

If you encounter compilation errors:
  1. Update Rust to the latest stable version: rustup update
  2. Clear the build cache: cargo clean
  3. Rebuild: cargo build

Type Errors in TypeScript

If you see TypeScript errors:
  1. Ensure you have the latest version: npm update htvg
  2. Check that your tsconfig.json includes "module": "ESNext" or "NodeNext"

Next Steps

Quick Start Guide

Now that HTVG is installed, follow our quick start guide to build your first project

Build docs developers (and LLMs) love