Skip to content

Workspace Class

A Workspace is the bridge between your JavaScript code and the EPANET (or EPANET-MSX) engine compiled to WebAssembly. It holds a virtual filesystem you can read and write — INP, RPT, OUT, hydraulics binary, MSX configuration — and it owns the loaded engine module that the Project class is built on top of.

Since epanet-js 0.9.0 the package ships two Workspace types:

  • The standard Workspace, imported from epanet-js, bundles a v2.3.5 WASM binary as a Base64 blob. Does not require additional setup and runs straight from a CDN or a plain HTML page.
  • The slim Workspace, imported from epanet-js/slim, contains only the Emscripten loader. It requires loading any of the published EPANET engine versions, with or without MSX. Engine versions can be loaded by importing them from epanet-js/engines/<version>. It allows switching engines at runtime and generates a smaller bundle.

Both expose the same file I/O methods. The difference is in how the engine is loaded.


Use when you want to drop the library into a page or CDN-served script and want to run the latest EPANET version.

import { Workspace } from "epanet-js";
const ws = new Workspace();
await ws.loadModule();

The WASM binary is embedded as a Base64 string in the JS bundle. This makes generates slightly larger download sizes, but requires no extra configuration.


The slim loader can target any of these engine entry points. Append -msx to the import path for the MSX-enabled variant:

Import pathEPANET versionMSX
epanet-js/engines/v2.22.2
epanet-js/engines/v2.2-msx2.2
epanet-js/engines/v2.32.3.0
epanet-js/engines/v2.3-msx2.3.0
epanet-js/engines/v2.3.1v2.3.52.3.1 – 2.3.5
epanet-js/engines/v2.3.1-msxv2.3.5-msx2.3.1 – 2.3.5
epanet-js/engines/masterOWA master snapshot
epanet-js/engines/master-msxOWA master snapshot
epanet-js/engines/devOWA dev snapshot
epanet-js/engines/dev-msxOWA dev snapshot

Pick v2.3.5 for the recommended stable engine; pick v2.2 for parity with desktop EPANET 2.2; pick master / dev to track upstream EPANET as it evolves.

Once the engine is loaded, available API methods on Project are determined by what that engine exports — see the version badges on every project function for the exact compatibility matrix.


The slim loader makes it easy to compare results between EPANET versions:

import { Workspace } from "epanet-js/slim";
import { Project } from "epanet-js";
async function runOn(version: "v2.2" | "v2.3.5") {
const ws = new Workspace();
await ws.loadModuleVersion(() => import(`epanet-js/engines/${version}`));
const project = new Project(ws);
ws.writeFile("net.inp", inpText);
project.open("net.inp", "report.rpt", "out.bin");
project.solveH();
// …read whatever values you need, then close
project.close();
return ws.readFile("report.rpt");
}

See the Choosing an engine version example for a complete walkthrough.


loadModule() standard only

Section titled “loadModule() ”

Asynchronously loads the embedded EPANET v2.3.5 WASM module. Must be called before passing the workspace to new Project(...).

loadModule(): Promise<void>

Only available on the standard Workspace from epanet-js. The slim workspace uses loadModuleVersion instead.

loadModuleVersion(engine) slim only

Section titled “loadModuleVersion(engine) ”

Loads the engine module returned by the supplied callback. The callback is typically an import() of an epanet-js/engines/* entry, or a previously-imported module wrapped in Promise.resolve.

loadModuleVersion(engine: () => Promise<EpanetEngine>): Promise<void>
ParameterTypeDescription
engine() => Promise<EpanetEngine>callback that resolves to one of the engine variants.

Writes data to a file in the workspace’s virtual filesystem. Used to make INP and MSX files available to the engine.

writeFile(path: string, data: string | Uint8Array): void
ParameterTypeDescription
pathstringpath inside the virtual filesystem (e.g. "net.inp").
datastring | Uint8Arrayfile contents. Strings are written as UTF-8.

Reads back a file from the virtual filesystem. Useful for retrieving the generated report (.rpt) or the binary output (.bin) after a simulation.

readFile(file: string): string;
readFile(file: string, encoding: "utf8"): string;
readFile(file: string, encoding: "binary"): Uint8Array;
ParameterTypeDescription
filestringpath of the file to read.
encoding”utf8” | “binary” (optional)"utf8" returns a string (default), "binary" returns a Uint8Array.

Translates a numeric EPANET error/warning code into the human-readable message stored in the engine.

getError(code: number): string

The integer version of the loaded engine (e.g. 20305 for v2.3.5). Useful for runtime feature detection.

get version(): number

true once loadModule() / loadModuleVersion() has resolved.

get isLoaded(): boolean