grafana/k6 · error

a function is expected as the second argument of…

Error message

a function is expected as the second argument of SharedArray's constructor

What it means

The second argument to new SharedArray must be a function that k6 invokes to build the contents. After ruling out async functions, the constructor calls sobek.AssertFunction on the value; if it is not callable (an array, string, object, undefined...), it throws this error before anything is stored.

Solutions

  1. Wrap the data in a function: new SharedArray('name', () => myArray).
  2. If the data lives in a JSON file, use a builder that reads and parses it: () => JSON.parse(open('./data.json')).
  3. Remember the returned value must itself be an array (see the 'only arrays' error) - the function wrapper is not optional.

Example fix

// before
const arr = new SharedArray('ids', [1, 2, 3]); // throws

// after
const arr = new SharedArray('ids', () => [1, 2, 3]);
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof builder !== 'function') {
  throw new Error(`SharedArray second argument must be a function, got ${typeof builder}`);
}
const arr = new SharedArray(name, builder);

Type guard

const isCallable = (v) => typeof v === 'function';

Prevention

When it happens

Trigger: 'new SharedArray("name", [1,2,3])' (passing the array directly), 'new SharedArray("name", data)' where data is a parsed object, or omitting the second argument so call.Argument(1) is undefined.

Common situations: Assuming SharedArray wraps an existing array value like a normal constructor; migrating from a const data = [...] and passing the variable; forgetting the builder-function syntax in the first attempt with the API.

Related errors


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

Appendix: source

Thrown at internal/js/modules/k6/data/data.go:93

	rt := d.vu.Runtime()

	if d.vu.State() != nil {
		common.Throw(rt, errors.New("new SharedArray must be called in the init context"))
	}

	name := call.Argument(0).String()
	if name == "" {
		common.Throw(rt, errors.New("empty name provided to SharedArray's constructor"))
	}
	val := call.Argument(1)

	if common.IsAsyncFunction(rt, val) {
		common.Throw(rt, errors.New(asyncFunctionNotSupportedMsg))
	}

	fn, ok := sobek.AssertFunction(val)
	if !ok {
		common.Throw(rt, errors.New("a function is expected as the second argument of SharedArray's constructor"))
	}

	builder := func() (sharedArray, error) { return getSharedArrayFromCall(rt, fn) }
	array, err := d.shared.loadOrStore(name, builder)
	if err != nil {
		common.Throw(rt, err)
	}

	return array.wrap(rt).ToObject(rt)
}

// RecordReader is the interface that wraps the action of reading records from a resource.
//
// The data module RecordReader interface is implemented by types that can read data that can be
// treated as records, from data sources such as a CSV file, etc.
type RecordReader interface {
	Read() (any, error)
}

View on GitHub (pinned to 01ffac6f24)