grafana/k6 · error
only arrays can be made into SharedArray
Error message
only arrays can be made into SharedArray
What it means
After invoking the SharedArray callback, k6 converts the return value to an object and requires its class to be 'Array'. Each element is then JSON.stringify'd into an immutable string held in shared memory. Any non-array return value (plain object, string, Map, or array-like) fails this check (the source itself carries a 'TODO better error').
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 93accf6570)
Solutions
- Make the callback return a plain Array: () => [...]
- Convert collections before returning: Array.from(map.values())
- Wrap single objects in an array if a one-element SharedArray is intended
Example fix
// before
new SharedArray('rows', () => ({ id: 1, name: 'x' }));
// after
new SharedArray('rows', () => [{ id: 1, name: 'x' }]); Defensive patterns
Strategy: type-guard
Validate before calling
const data = buildData();
if (!Array.isArray(data)) throw new Error('SharedArray builder must return an Array, got ' + (data && data.constructor)?.name);
new SharedArray('name', () => data); Type guard
/** @param {unknown[]} arr */
function assertPlainArray(arr) {
if (!Array.isArray(arr)) {
throw new TypeError(`expected Array, got ${Object.prototype.toString.call(arr)}`);
}
return arr;
}
new SharedArray('name', () => assertPlainArray(loadRows())); Prevention
- Remember SharedArray elements are JSON.stringify'd -- design payloads as arrays of flat records
- Convert Maps/Sets with Array.from(...) inside the builder
- Test the builder standalone in init before wiring it into SharedArray
When it happens
Trigger: Callback returns an object literal (() => ({a:1})), a string, undefined, or a Map/Set instead of a plain Array; returning an array-like object with a length property but class 'Object'.
Common situations: Parsing CSV/JSON sources that yield objects or dictionaries rather than lists; refactoring the builder so it accidentally returns the result of a mutation method or a wrapper type.
Related errors
- exported 'setup' must be a function
- exported 'teardown' must be a function
- no exported functions in script
- open() can't be used with an empty filename
- predicate function is not callable
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/ba4df91c57d5d0b0.
Report an issue: GitHub.