{"record":{"id":"90a8e8350ff973a3","repo":"grafana/k6","slug":"couldn-t-load-module-instance-while-resolving-iden","errorCode":null,"errorMessage":"couldn't load module instance while resolving identifier %q - this is a k6 bug , please report it (https://github.com/grafana/k6/issues)","messagePattern":"couldn't load module instance while resolving identifier %q - this is a k6 bug , please report it \\(https://github\\.com/grafana/k6/issues\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/bundle.go","lineNumber":75,"sourceCode":"\n\tmainModule   sobek.ModuleRecord\n\tmoduleVUImpl *moduleVUImpl\n}\n\nfunc (bi *BundleInstance) getCallableExport(name string) sobek.Callable {\n\tfn, ok := sobek.AssertFunction(bi.getExported(name))\n\t_ = ok // TODO maybe return it\n\treturn fn\n}\n\nfunc (bi *BundleInstance) getExported(name string) sobek.Value {\n\tre, ambigiuous := bi.mainModule.ResolveExport(name)\n\tif ambigiuous || re == nil {\n\t\treturn nil\n\t}\n\tmoduleInstance := bi.Runtime.GetModuleInstance(re.Module)\n\tif moduleInstance == nil {\n\t\tpanic(fmt.Sprintf(\"couldn't load module instance while resolving identifier %q - this is a k6 bug \"+\n\t\t\t\", please report it (https://github.com/grafana/k6/issues)\", re.BindingName))\n\t}\n\n\treturn moduleInstance.GetBindingValue(re.BindingName)\n}\n\n// NewBundle creates a new bundle from a source file and a filesystem.\nfunc NewBundle(\n\tpiState *lib.TestPreInitState, src *loader.SourceData, filesystems map[string]fsext.Fs, mr *modules.ModuleResolver,\n) (*Bundle, error) {\n\treturn newBundle(piState, src, filesystems, lib.Options{}, true, mr)\n}\n\nfunc newBundle(\n\tpiState *lib.TestPreInitState, src *loader.SourceData, filesystems map[string]fsext.Fs,\n\toptions lib.Options, updateOptions bool, // TODO: try to figure out a way to not need both\n\tmoduleResolver *modules.ModuleResolver,\n) (*Bundle, error) {","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/bundle.go#L57-L93","documentation":"During bundle creation, resolving an exported binding walks the ESM module graph and fetches the live module instance from the runtime. If the resolved export's module has no instance, k6 panics with an explicit 'this is a k6 bug, please report' message — by construction every resolved export should belong to an instantiated module, so this indicates an engine defect, not user error, though exotic scripts can provoke it.","triggerScenarios":"Complex ESM graphs: circular imports combined with re-exports, import.resolveExport finding a binding in a module that was never instantiated, or mixed CommonJS/ESM bridging in k6 >= v0.53's Sobek module runtime. Fires at script load/bundling time (k6 run/archive) when getExported resolves a top-level identifier like the default export.","commonSituations":"Upgrading k6 across the Goja->Sobek module-runtime transition and hitting a regression; scripts using barrel files (index.js re-exporting many modules) with cycles; local imports (./file.js) plus remote imports mixed in one graph; cutting-edge TypeScript/transpiled output patterns. The error text itself directs users to the GitHub issue tracker because k6 maintainers want reproducers.","solutions":["Update to the latest k6 patch release — several Sobek module-resolution bugs were fixed after the transition","Minimize the repro (one import chain) and report it at https://github.com/grafana/k6/issues with the script and k6 version, as the panic requests","Work around by restructuring imports: break circular dependencies, import directly from the defining module instead of a re-exporting barrel, or re-export with explicit 'export { x } from ...'","If a transpiler is involved, simplify its module output to plain ESM and retest"],"exampleFix":"// before: circular re-export chain that can hit the bug\n// a.js: export { helper } from './b.js'\n// b.js: import { other } from './a.js'; export function helper(){ return other(); }\n\n// after: break the cycle; import from the defining module directly\n// a.js: export function other(){ ... }\n// b.js: import { other } from './a.js'; export function helper(){ return other(); }","handlingStrategy":"try-catch","validationCode":"// not user-preventable; minimize the module graph instead\n// avoid circular re-export chains before bundling\n// check: k6 inspect script.js runs bundle creation cheaply in CI","typeGuard":null,"tryCatchPattern":"// Go (embedding k6 as a library): wrap NewBundle\nb, err := js.NewBundle(piState, src, fs, resolver)\nif err != nil { return fmt.Errorf(\"bundle creation failed (possible k6 bug, see https://github.com/grafana/k6/issues): %w\", err) }","preventionTips":["Pin a known-good k6 version; retest bundle-sensitive scripts on upgrades","Avoid circular imports and deep re-export barrel files in ESM scripts","Import from defining modules directly rather than through index re-exports","When hit, minimize the import chain and report it upstream with the version"],"tags":["k6-bug","esm","modules","panic","bundle"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}