clockworklabs/SpacetimeDB · error · TypeError
Math.random is not available in SpacetimeDB modules. Use…
Error message
Math.random is not available in SpacetimeDB modules. Use ctx.random instead.
What it means
The V8 module host installs a builtin that deletes Math.random and reinstalls it as a throwing getter. SpacetimeDB reducers must be deterministic — database state is derived from ordered reducer execution — so unseeded randomness is banned. Modules get a deterministic, seed-based generator on ctx.random instead (seeded per call from the module context).
Solutions
- Replace Math.random() with the context API: ctx.random() (float in [0,1)), ctx.random.integerInRange(min, max), ctx.random.uint32(), ctx.random.fill(Uint8Array), ctx.random.bigintInRange(min, max)
- Derive ids from ReducerContext data or ctx.random.fill(new Uint8Array(16)) instead of random-string helpers
- Audit module dependencies for Math.random usage and swap in deterministic equivalents
- Test modules under the SpacetimeDB host during development, not only under Node, so violations surface early
Example fix
// before
export const rollDice = (ctx: ReducerContext) => {
const roll = Math.floor(Math.random() * 6) + 1; // TypeError in module
};
// after
export const rollDice = (ctx: ReducerContext) => {
const roll = ctx.random.integerInRange(1, 6); // deterministic, seeded
}; Defensive patterns
Strategy: fallback
Validate before calling
# CI guard: reject module sources that reference the deleted builtin
# (the getter throws at access time, so no runtime probe is safe)
grep -rn "Math\\.random" module-src/ && { echo 'Math.random is forbidden in SpacetimeDB modules'; exit 1; } || exit 0 Try / catch
// acceptable only while porting; production code must be deterministic:
let r: number;
try {
r = Math.random();
} catch {
r = ctx.random();
} Prevention
- Always read randomness through ctx.random (callable plus integerInRange/uint32/fill/bigintInRange)
- Audit bundled npm dependencies for Math.random before publishing the module
- Run module tests inside the SpacetimeDB host, not plain Node
When it happens
Trigger: Evaluating or calling Math.random anywhere in a TypeScript module (reducer, scheduled function, client_authority/init) running under the SpacetimeDB V8 host; accessing Math.random indirectly through third-party npm code — uuid-style generators, lodash.sample, chance.js, game RNG helpers — imported into the module.
Common situations: Porting game or server logic that used Math.random; libraries that mint random ids; code that passes under plain Node during development but throws once published and invoked in spacetimedb.
Related errors
- Option 'ignoreBOM' not supported
- Option 'stream' not supported
- The encoding label provided is invalid
- must export schema as default export
- Brotli compression is not supported by the runtime. Please…
AI-assisted analysis of clockworklabs/SpacetimeDB@6dee26c6ef (2026-08-20).
Data as JSON: /api/errors/e7c15a1a2d45cacc.
Report an issue: GitHub.
Appendix: source
Thrown at crates/core/src/host/v8/builtins/delete_math_random.js:6
delete Math.random;
Object.defineProperty(Math, 'random', {
enumerable: false,
configurable: true,
get() {
throw new TypeError(
'Math.random is not available in SpacetimeDB modules. Use ctx.random instead.'
);
},
});
View on GitHub (pinned to 6dee26c6ef)