grafana/k6 · error

height must be a number, got

Error message

height must be a number, got %s

What it means

Thrown by parseSize when the 'height' key of page.setViewportSize(size) is present but its value's runtime kind is neither Int64 nor Float64. Like width, only real numbers are accepted; strings such as '720' or '720px', booleans, and objects are rejected with the offending reflect.Kind printed.

Solutions

  1. Coerce both dimensions with Number() before calling setViewportSize.
  2. Validate the parsed config once at script startup and fail fast with a clear message.
  3. Use numeric literals for static viewport sizes.
  4. Sanitize external config: strip quotes/units and check Number.isFinite.

Example fix

// before
await page.setViewportSize({ width: cfg.width, height: cfg.height }); // cfg.height === "720"

// after
await page.setViewportSize({ width: Number(cfg.width), height: Number(cfg.height) });
Defensive patterns

Strategy: type-guard

Validate before calling

const h = Number(size.height);
if (!Number.isFinite(h)) throw new TypeError(`height must be a number, got ${typeof size.height}`);
await page.setViewportSize({ width: Number(size.width), height: h });

Type guard

const isPlainNumber = (v) => typeof v === 'number' && Number.isFinite(v);

Prevention

When it happens

Trigger: page.setViewportSize({ width: 1280, height: '720' }); height sourced from JSON config where it was serialized as a string; height left as an empty string after env var defaulting; height passed as an array or object.

Common situations: Mixed-type config objects (one dimension numeric, the other string); environment-variable-driven responsive testing; configs authored in YAML/JSON where quotes were added around the number.

Related errors


AI-assisted analysis of grafana/k6@8d06114777 (2026-08-18). Data as JSON: /api/errors/7794065461b2fe28. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/browser/page_mapping.go:1213

	for _, k := range obj.Keys() {
		v := obj.Get(k)
		if k6common.IsNullish(v) {
			continue
		}
		switch k {
		case "width":
			switch v.ExportType().Kind() {
			case reflect.Int64, reflect.Float64:
				size.Width = v.ToFloat()
			default:
				return nil, fmt.Errorf("width must be a number, got %s", v.ExportType().Kind())
			}
		case "height":
			switch v.ExportType().Kind() {
			case reflect.Int64, reflect.Float64:
				size.Height = v.ToFloat()
			default:
				return nil, fmt.Errorf("height must be a number, got %s", v.ExportType().Kind())
			}
		}
	}

	return size, nil
}

// parsePageEmulateMediaOptions parses the page emulate media options from a Sobek value.
//
//nolint:unparam
func parsePageEmulateMediaOptions(
	rt *sobek.Runtime, opts sobek.Value, defaults *common.PageEmulateMediaOptions,
) (*common.PageEmulateMediaOptions, error) {
	if k6common.IsNullish(opts) {
		return defaults, nil
	}

	obj := opts.ToObject(rt)

View on GitHub (pinned to 8d06114777)