grafana/k6 · error

must be a number

Error message

must be a number

What it means

exportInt requires the JS value's export type to be exactly reflect.Int64 - a genuine number. Strings like '10', booleans, or objects all fail with 'must be a number' (the source comment explains ExportTo was tried first but let strings slip through, hence the strict check). File.seek prefixes it, e.g. 'seek() failed; reason: the whence argument must be a number'.

Solutions

  1. Convert explicitly with Number() or parseInt(): file.seek(Number(cfg.offset), SeekMode.Start).
  2. Use literal numbers or computed numeric values for both arguments.
  3. Remember k6 --env values are strings: wrap them with Number() at the call site.

Example fix

// before
const off = __ENV.SEEK_OFFSET; // string like '128'
await file.seek(off, SeekMode.Start); // rejects: must be a number

// after
await file.seek(Number(__ENV.SEEK_OFFSET), SeekMode.Start);
Defensive patterns

Strategy: type-guard

Validate before calling

const isInt = (v) => Number.isInteger(Number(v)) && v !== null && v !== '' && typeof v !== 'string';
// simpler strict check:
const isStrictInt = (v) => typeof v === 'number' && Number.isInteger(v);
if (!isStrictInt(offset)) throw new TypeError('offset must be a number');

Type guard

const isStrictInt = (v) => typeof v === 'number' && Number.isInteger(v);

Prevention

When it happens

Trigger: file.seek('10', SeekMode.Start) or file.seek(0, '0') - numeric strings read from JSON config, CSV, URL params, or parseInt-less user input. Note the whence check then also validates the value against the three SeekMode constants, so out-of-range numbers fail with a different 'must be a SeekMode' error.

Common situations: Offsets loaded from a JSON/CSV config file arrive as strings in JS; passing process.env or CLI-supplied (--env) values, which are strings in k6; copy-pasting code from a language where seek takes an enum name string.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/13d5a4c690a4ce1f. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/experimental/fs/module.go:352

	uint8ArrayConstructor := rt.Get("Uint8Array")
	if isUint8Array := o.Get("constructor").SameAs(uint8ArrayConstructor); !isUint8Array {
		return false
	}

	return true
}

func exportInt(v sobek.Value) (int64, error) {
	if common.IsNullish(v) {
		return 0, errors.New("cannot be null or undefined")
	}

	// We initially tried using `ExportTo` with a int64 value argument, however
	// this led to a string passed as argument not being an error.
	// Thus, we explicitly check that the value is a number, by comparing
	// its export type to the type of an int64.
	if v.ExportType().Kind() != reflect.Int64 {
		return 0, errors.New("must be a number")
	}

	return v.ToInteger(), nil
}

View on GitHub (pinned to 01ffac6f24)