grafana/k6 · error
only arrays can be made into SharedArray
Error message
only arrays can be made into SharedArray
What it means
k6 calls your SharedArray builder once and inspects the returned value's JS class via obj.ClassName(). SharedArray only supports plain JavaScript arrays because each element gets JSON.stringify-ed into a packed Go slice that VUs mmap-style share; an object, Map, typed array, string or undefined therefore fails with this error (the source even carries a 'TODO better error').
Solutions
- Make the builder return a plain array literal: if the JSON is an object, return JSON.parse(open('./data.json')).users.
- Add (or fix) the return statement in the builder function.
- Flatten nested structures into an array of records before returning; per-item fields are fine, the top-level value must be an Array.
Example fix
// before: data.json is { "users": [ ... ] }
new SharedArray('users', () => JSON.parse(open('./data.json'))); // throws: only arrays
// after
new SharedArray('users', () => JSON.parse(open('./data.json')).users); Defensive patterns
Strategy: validation
Validate before calling
// dry-run the builder once at init to fail fast with a clear message
const build = () => JSON.parse(open('./data.json')).items; // may be an object
const probe = build();
if (!Array.isArray(probe)) {
throw new Error(`SharedArray builder must return an array, got ${Object.prototype.toString.call(probe)}`);
}
const arr = new SharedArray('data', build); Type guard
const returnsArray = (fn) => Array.isArray(fn());
Prevention
- Normalize data files to top-level JSON arrays.
- Dry-run builders at init so bad shapes fail before the test starts.
When it happens
Trigger: A builder returning an object (e.g. returning JSON.parse of an object document), a Map/Set, a string, or implicitly undefined (missing return statement). The check happens after the builder has run, so side effects inside the builder have already executed.
Common situations: Data file is a JSON object ({"users": [...]}) rather than an array - the natural builder returns the whole object; forgetting the return keyword in the builder; returning a generator or iterator result.
Related errors
- a function is expected as the second argument of…
- empty name provided to SharedArray's constructor
- cannot be null or undefined
- env var ' ' is not a valid boolean value
- invalid environment variable name
AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18).
Data as JSON: /api/errors/ba4df91c57d5d0b0.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/data/data.go:206
return arr, nil
}
arr, err := builder()
if err != nil {
return arr, err
}
s.data[name] = arr
return arr, nil
}
func getSharedArrayFromCall(rt *sobek.Runtime, call sobek.Callable) (sharedArray, error) {
sobekValue, err := call(sobek.Undefined())
if err != nil {
return sharedArray{}, err
}
obj := sobekValue.ToObject(rt)
if obj.ClassName() != "Array" {
return sharedArray{}, errors.New("only arrays can be made into SharedArray") // TODO better error
}
arr := make([]string, obj.Get("length").ToInteger())
stringifyFunc, _ := sobek.AssertFunction(rt.GlobalObject().Get("JSON").ToObject(rt).Get("stringify"))
var val sobek.Value
for i := range arr {
val, err = stringifyFunc(sobek.Undefined(), obj.Get(strconv.Itoa(i)))
if err != nil {
return sharedArray{}, err
}
arr[i] = val.String()
}
return sharedArray{arr: arr}, nil
}
View on GitHub (pinned to 01ffac6f24)