Livecode Npm Module

You can include a WebAssembly (WASM) binary exported from LiveCode inside an npm module, allowing you to reuse it across projects or publish it to npm. This page explains how.

# Folder Layout Create your project folder like this:

my-livecode-module/ ├─ package.json ├─ index.js ├─ livecode.wasm

In your `package.json`, include the `.wasm` file so npm publishes it:

{ "files": ["index.js", "livecode.wasm"] }

# Loading the WASM You can load your LiveCode WASM file differently depending on the environment.

### Node.js Example

import fs from "fs"; import { fileURLToPath } from "url"; import { dirname, join } from "path"; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); export async function loadLivecode() { const bytes = await fs.promises.readFile(join(__dirname, "livecode.wasm")); const { instance } = await WebAssembly.instantiate(bytes, {}); return instance.exports; }

# Browser Example

export async function loadLivecode() { const response = await fetch(new URL("./livecode.wasm", import.meta.url)); const { instance } = await WebAssembly.instantiateStreaming(response, {}); return instance.exports; }

# Universal Loader You can combine both approaches for hybrid Node + Browser use:

export async function loadLivecode() { const isNode = typeof process !== "undefined" && process.versions?.node; if (isNode) { const fs = await import("fs/promises"); const { fileURLToPath } = await import("url"); const { dirname, join } = await import("path"); const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const bytes = await fs.readFile(join(__dirname, "livecode.wasm")); const { instance } = await WebAssembly.instantiate(bytes, {}); return instance.exports; } else { const response = await fetch(new URL("./livecode.wasm", import.meta.url)); const { instance } = await WebAssembly.instantiateStreaming(response, {}); return instance.exports; } }

# Using the Module After publishing or linking your module:

import { loadLivecode } from "my-livecode-module"; const livecode = await loadLivecode(); const result = livecode.processJSON(`{"foo": "bar"}`); console.log(result);

# Notes - LiveCode may generate both `.wasm` and a JS loader file. If so, import the generated JS file instead of instantiating manually. - Node 18+ supports `fetch()` natively, so the browser loader will work there too. - Keep the `.wasm` file small and self-contained for easier npm distribution.