grafana/k6 · error

error parsing script options

Error message

error parsing script options: %w

What it means

populateExports wraps json.MarshalIndent failing on the script's exported options object (v.Export() of the 'options' export). Fires when the exported options contain values that cannot be converted to JSON (e.g. functions, cyclical references, or non-serializable JS objects).

Solutions

  1. Make the exported options a plain object of primitive/serializable values
  2. Remove functions or complex objects from the options export
  3. Ensure options only contains keys k6 understands (scenarios, thresholds, etc.)
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at internal/js/bundle.go:205 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18). Data as JSON: /api/errors/e4848f8b5748431f. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/bundle.go:205

	var err error
	ch := make(chan struct{})
	bi.mainModule.GetExportedNames(func(names []string) {
		defer close(ch)
		for _, k := range names {
			v := bi.getExported(k)
			if _, ok := sobek.AssertFunction(v); ok && k != consts.Options {
				b.callableExports[k] = struct{}{}
				continue
			}
			switch k {
			case consts.Options:
				if !updateOptions || v == nil {
					continue
				}
				var data []byte
				data, err = json.MarshalIndent(v.Export(), "", "  ")
				if err != nil {
					err = fmt.Errorf("error parsing script options: %w", err)
					return
				}
				dec := json.NewDecoder(bytes.NewReader(data))
				dec.DisallowUnknownFields()
				if err = dec.Decode(&b.Options); err != nil {
					if uerr := json.Unmarshal(data, &b.Options); uerr != nil {
						// Beautify the error so we can try to show the user the key and potential value that is failing to parse
						uerr = beautifyOptionsJSONUnmarshalError(data, uerr)
						err = errext.WithAbortReasonIfNone(
							errext.WithExitCodeIfNone(uerr, exitcodes.InvalidConfig),
							errext.AbortedByScriptError,
						)
						return
					}
					b.preInitState.Logger.WithError(err).Warn("There were unknown fields in the options exported in the script")
					err = nil
				}
			case consts.SetupFn:

View on GitHub (pinned to 3fcf5388d7)