grafana/k6 · error

initializing the main module hasn't finished, this is a bug…

Error message

initializing the main module hasn't finished, this is a bug in k6 please report it

What it means

An internal invariant in the bundle init path (internal/js/bundle.go:372): after the VU event loop finishes running the main module's top-level code, result.Result() must report completion. If the module future is not finished, k6 returns this explicit 'this is a bug in k6 please report it' error rather than continuing in an undefined state. It typically surfaces with unusual top-level await / async module-init patterns.

Solutions

  1. Upgrade to the latest k6 release — this class of bug gets fixed upstream.
  2. Report the issue at github.com/grafana/k6/issues with the script and k6 version.
  3. As a workaround, move top-level await into an async setup() function or wrap initialization in an explicitly awaited call.

Example fix

// before (module top-level)
const data = await fetchData();
export default function () { ... }
// after
export async function setup() { return await fetchData(); }
export default function (data) { ... }
Defensive patterns

Strategy: fallback

Prevention

When it happens

Trigger: Initializing a script whose main module uses top-level await, async imports, or timing patterns that leave the module-init promise unresolved when the event loop drains; often a regression in a specific k6 version rather than a user config error.

Common situations: Upgrading k6 and having previously-working scripts with top-level await hit the invariant; scripts mixing module-level async initialization with imported ESM bundles.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at internal/js/bundle.go:375

		var err error
		result, err = modSys.RunSourceData(b.sourceData)
		return err
	}

	call, _ := sobek.AssertFunction(vuImpl.runtime.ToValue(callback))

	err = vuImpl.eventLoop.Start(func() error {
		_, err := call(nil)
		return err
	})

	<-initDone

	if err == nil {
		var finished bool
		bi.mainModule, finished, err = result.Result()
		if !finished {
			return nil, errors.New("initializing the main module hasn't finished, this is a bug in k6 please report it")
		}
	}

	if err != nil {
		if exception, ok := errors.AsType[*sobek.Exception](err); ok {
			err = &scriptExceptionError{inner: exception}
		}
		return nil, err
	}

	// If we've already initialized the original VU init context, forbid
	// any subsequent VUs to open new files
	if vuID == 0 {
		allowOnlyOpenedFiles(b.filesystems["file"])
	}

	rt.SetRandSource(common.NewRandSource())

View on GitHub (pinned to 3fcf5388d7)