BabylonJS/Babylon.js · error

width must be a finite positive integer.

Error message

width must be a finite positive integer.

What it means

Thrown by the 'screenshot' command when args.width is provided but is not a finite positive integer: Number(args.width) must pass Number.isFinite, be > 0, and be an integer. Width defines the output pixel width of the screenshot.

Source

Thrown at packages/dev/inspector-v2/src/services/cli/screenshotCommandService.ts:76

                    }
                } else {
                    camera = scene.frameGraph ? FrameGraphUtils.FindMainCamera(scene.frameGraph) : scene.activeCamera;
                }

                if (!camera) {
                    throw new Error("No camera available for screenshot.");
                }

                const precision = args.precision !== undefined ? Number(args.precision) : 1;
                if (!Number.isFinite(precision) || precision <= 0) {
                    throw new Error("precision must be a finite number greater than 0.");
                }

                let width: number | undefined;
                if (args.width !== undefined) {
                    width = Number(args.width);
                    if (!Number.isFinite(width) || width <= 0 || !Number.isInteger(width)) {
                        throw new Error("width must be a finite positive integer.");
                    }
                }

                let height: number | undefined;
                if (args.height !== undefined) {
                    height = Number(args.height);
                    if (!Number.isFinite(height) || height <= 0 || !Number.isInteger(height)) {
                        throw new Error("height must be a finite positive integer.");
                    }
                }
                const screenshotSize = width !== undefined && height !== undefined ? { width, height, precision } : { precision };

                // Omit fileName to get data URL back without triggering a download.
                const dataUrl = await CreateScreenshotUsingRenderTargetAsync(engine, camera, screenshotSize, "image/png");

                // Strip the data URI prefix to return raw base64, which is what AI agent APIs expect.
                const commaIndex = dataUrl.indexOf(",");
                return commaIndex !== -1 ? dataUrl.substring(commaIndex + 1) : dataUrl;

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Pass width as a positive integer pixel count, e.g. width: 800.
  2. Round computed values before passing: width: Math.round(desiredWidth).
  3. Omit width (and height) to keep the current render resolution.

Example fix

// before
await execute("screenshot", { width: "800px" });
// after
await execute("screenshot", { width: 800 });
Defensive patterns

Strategy: validation

Validate before calling

if (args.width !== undefined) {
  const w = Number(args.width);
  if (!Number.isFinite(w) || w <= 0 || !Number.isInteger(w)) throw new RangeError(`width must be a positive integer, got: ${args.width}`);
}

Type guard

function isValidWidth(v: unknown): v is number { return typeof v === "number" && Number.isInteger(v) && v > 0; }

Try / catch

try {
  await screenshot({ width });
} catch (e) {
  if (e instanceof Error && e.message.startsWith("width must be")) {
    return screenshot({ width: Math.max(1, Math.round(Number(width) || 0)) });
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling screenshot with width=0, width=-100, width=12.5, width="abc", or width=Infinity.

Common situations: Passing a CSS width like "800px" (Number gives NaN); passing a fractional scale factor (0.5) where pixels are expected; forgetting Math.round on a computed value.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/342ca65f35218f41. Report an issue: GitHub.