Development Setup#
This document explains how you can add the webvis library and its TypeScript types to your project, in the browser or in Node.js.
Using webvis in the browser
The webvis library is not installed from a package registry. Instead, every
instant3Dhub serves the webvis version that matches it, at
<hubURL>/repo/webvis/webvis.js. You can load it with a regular script tag:
<script src="https://your-hub.example.com/repo/webvis/webvis.js?next"></script>
The script defines two globals: webvis, the API documented in this reference,
and webvisUI, for the default UI elements and their settings. It also registers
the default webvis web components such as <webvis-full> and <webvis-viewer>.
A few things to know:
- Always add
?next. Without it, webvis starts in legacy mode, which should not be used by new apps. See Legacy Mode for its removal and the?nextparameter from 3.15.0 on. - Use the global, not
import.webvis.jsis a classic script, not an ES module, and has no exports. You can directly use thewebvisglobal it creates.
<script src="https://your-hub.example.com/repo/webvis/webvis.js?next"></script>
<script type="module">
const context = await webvis.requestContext("my-context");
const viewer = context.createViewer("my-viewer", document.getElementById("my-canvas"));
console.log("webvis", webvis.about.version);
</script>
Getting the types
The TypeScript types are published on npm as
@types/webvis. Their version
follows the webvis release, so install the one that matches the webvis version
of your hub:
npm install --save-dev @types/webvis@3.13
If the types are newer than your hub, TypeScript will accept APIs that your hub's webvis does not have yet.
The package declares the global webvis namespace, matching the global the
script creates. There is nothing to import, so TypeScript only knows about the
namespace if you add webvis to compilerOptions.types in your tsconfig.json:
{
"compilerOptions": {
"types": ["webvis"]
}
}
This is required from TypeScript 6 on, where types defaults to an empty list.
Older versions include all installed @types packages automatically, unless
types is set. Alternatively, reference the types from a source file:
/// <reference types="webvis" />
Then use the webvis namespace for both values and types:
const context: webvis.ContextAPI | undefined = await webvis.requestContext("my-context", {
hubURL: "https://your-hub.example.com/"
});
if (!context) {
throw new Error("webvis context could not be created");
}
const canvas: HTMLCanvasElement = document.querySelector<HTMLCanvasElement>("#my-canvas");
const viewer: webvis.ViewerAPI = context.createViewer("my-viewer", canvas);
const nodeID: number = context.add({ dataURI: "urn:x-i3d:examples:x3d:v8" });
context.setProperty(nodeID, webvis.Property.ENABLED, true);
Do not write import ... from "webvis": there is no such module at runtime, the
types describe the global.
Using webvis in Node.js
Besides webvis.js for the browser, every instant3Dhub also serves the webvis
core as an ES module for Node.js, at <hubURL>/repo/webvis/webvis.core.mjs. It
contains the same API as the webvis global, but without the UI elements and
without rendering: there is no webvisUI, and you cannot create viewers. Use it
for headless tasks such as loading models, querying the scene or working with
collaboration sessions.
A few things to know:
- Node.js 26 is required. Other Node.js versions are not supported.
- Load it from your hub. Like
webvis.js, the module is not installed from a package registry. Load it from the hub you connect to, so that the webvis version always matches the hub. - Node.js cannot import from
http(s)URLs by itself. Fetch the module first, then hand its source to Node.js with amodule.registerHookshook.webvis.core.mjshas no imports of its own, so this one file is all you need to fetch. Do not usemodule.registerfor this: it is deprecated since Node.js 26. - Use a dynamic
import(). Staticimportstatements are resolved before any code runs, so before the hook is registered. - Always pass
hubURL. In Node.js there is no page that webvis could take the hub URL from.
For example, main.mts:
import { registerHooks } from "node:module";
const hubURL = "https://your-hub.example.com/";
const webvisURL = new URL("repo/webvis/webvis.core.mjs", hubURL).href;
const response = await fetch(webvisURL);
if (!response.ok) {
throw new Error(`Could not load ${webvisURL}: ${response.status} ${response.statusText}`);
}
const source = await response.text();
registerHooks({
resolve(specifier, context, nextResolve) {
return specifier === webvisURL
? { url: webvisURL, shortCircuit: true }
: nextResolve(specifier, context);
},
load(url, context, nextLoad) {
return url === webvisURL
? { format: "module", source, shortCircuit: true }
: nextLoad(url, context);
}
});
const webvis: typeof globalThis.webvis = await import(webvisURL);
console.log("webvis", webvis.about.version);
const context: webvis.ContextAPI | undefined = await webvis.requestContext("my-context", { hubURL });
if (!context) {
throw new Error("webvis context could not be created");
}
const nodeID: number = context.add({ dataURI: "urn:x-i3d:examples:x3d:v8" });
context.setProperty(nodeID, webvis.Property.ENABLED, true);
// ... work with the context ...
await webvis.removeContext("my-context");
Node.js runs TypeScript files directly, so you can start it with:
node main.mts
For the types, install @types/webvis as described in
Getting the types, plus @types/node, and list both in
your tsconfig.json:
{
"compilerOptions": {
"module": "nodenext",
"types": ["node", "webvis"]
}
}
The example uses top-level await, so it has to run as an ES module. The
.mts extension makes it one, whatever your package.json says. If you prefer
.ts, add "type": "module" to your package.json instead.
await import() returns an untyped module, so annotate the result with
typeof globalThis.webvis to get the types of the webvis namespace. Naming the
constant webvis lets you use the namespace's types, such as
webvis.ContextAPI, just like in the browser.
The hooks are synchronous, which is why the module is fetched before
registerHooks is called. Because the module keeps its hub URL, error messages
and stack traces point to webvis.core.mjs on your hub.