grafana/k6 · error
getting scenario information outside of the VU context is no
Error message
getting scenario information outside of the VU context is not supported
What it means
The k6/execution module exposes exec.scenario and exec.vu through property accessors that read live VU state (vu.State()). That state only exists while a VU is executing iterations; in the init context (top-level script code) it is nil, so accessing scenario or VU info there throws errRunInInitContext.
Source
Thrown at internal/js/modules/k6/execution/execution.go:74
common.Throw(rt, err)
}
}
defProp("instance", mi.newInstanceInfo)
defProp("scenario", mi.newScenarioInfo)
defProp("test", mi.newTestInfo)
defProp("vu", mi.newVUInfo)
mi.obj = o
return mi
}
// Exports returns the exports of the execution module.
func (mi *ModuleInstance) Exports() modules.Exports {
return modules.Exports{Default: mi.obj}
}
var errRunInInitContext = errors.New("getting scenario information outside of the VU context is not supported")
// newScenarioInfo returns a sobek.Object with property accessors to retrieve
// information about the scenario the current VU is running in.
func (mi *ModuleInstance) newScenarioInfo() (*sobek.Object, error) {
rt := mi.vu.Runtime()
vuState := mi.vu.State()
if vuState == nil {
return nil, errRunInInitContext
}
getScenarioState := func() *lib.ScenarioState {
ss := lib.GetScenarioState(mi.vu.Context())
if ss == nil {
common.Throw(rt, errRunInInitContext)
}
return ss
}
si := map[string]func() any{View on GitHub (pinned to 93accf6570)
Solutions
- Move exec.scenario / exec.vu access inside the default exported function (or functions it calls per iteration)
- For init-time branching, use the options/scenarios objects from k6/options or the exported options variable instead
- Guard shared helpers: only touch exec when running inside an iteration (pass the info in as arguments)
Example fix
// before (init context -- throws)
import exec from 'k6/execution';
import { scenario } from 'k6/execution';
const tag = `scenario=${exec.scenario.name}`;
// after
import exec from 'k6/execution';
export default function () {
const tag = `scenario=${exec.scenario.name}`;
// ...
} Defensive patterns
Strategy: try-catch
Try / catch
import exec from 'k6/execution';
function safeScenarioName() {
try {
return exec.scenario.name;
} catch (e) {
if (String(e.message).includes('outside of the VU context')) return 'init';
throw e;
}
} Prevention
- Treat exec.vu and exec.scenario as iteration-phase-only APIs; never read them at module scope
- Pass scenario name into shared helpers as a parameter captured inside the default function
- For init-time decisions use the exported options object or k6/options instead
When it happens
Trigger: Reading exec.scenario.name, exec.vu.idInTest, or exec.instance at the top level of the script; calling a helper that touches exec from setup()/teardown() or other non-iteration phases; using exec during module-scope constant initialization.
Common situations: Trying to compute per-scenario thresholds, tags, or config at file scope; logging or branching on scenario name before the default function; sharing a helper between init and VU phases that unconditionally dereferences exec.
Related errors
- open() can't be used with an empty filename
- new SharedArray must be called in the init context
- SharedArray constructor does not support async functions as
- csv Parser constructor must be called in the init context
- open() failed; reason: unable to access the file system
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/5ead4fb8e90be5cd.
Report an issue: GitHub.