The official MuJoCo WebAssembly module
npm install mujoco-jsThis package contains the pre-compiled WebAssembly module and JavaScript bindings for MuJoCo.
``sh`
npm install mujoco-js
`javascript
import type { MainModule, MjData, MjModel } from "mujoco-js";
import loadMujoco from 'mujoco-js';
const mujoco: MainModule = await loadMujoco();
// The Emscripten file system (FS) needs to be mounted to be used.
(mujoco as any).FS.mkdir('/working');
(mujoco as any).FS.mount((mujoco as any).MEMFS, { root: '.' }, '/working');
console.log('Mounted virtual file system.');
const xmlContent =
;
mujoco.FS.writeFile('/working/hello.xml', xmlContent);
console.log('Wrote hello.xml to virtual file system.');
let model: MjModel | undefined;
let data: MjData | undefined;
try {
model = mujoco.MjModel.loadFromXML('/working/hello.xml');
if (!model) {
throw new Error('Failed to load model from XML');
}
console.log('Model loaded:', model);
data = new mujoco.MjData(model);
if (!data) {
throw new Error('Failed to create MjData');
}
console.log('Data created:', data);
console.log('SUCCESS: Model and Data exist!');
} catch (error) {
console.error("An error occurred during MuJoCo initialization:", error);
} finally {
model?.delete();
data?.delete();
mujoco.FS.unmount('/working');
console.log('Cleaned up resources.');
}
``