{"record":{"id":"9b6ace89f8f12bc8","repo":"grafana/k6","slug":"somehow-a-commonjs-module-is-not-wrapped-in-a-func","errorCode":null,"errorMessage":"Somehow a CommonJS module is not wrapped in a function - this is a k6 bug, please report it (https://github.com/grafana/k6/issues)","messagePattern":"Somehow a CommonJS module is not wrapped in a function - this is a k6 bug, please report it \\(https://github\\.com/grafana/k6/issues\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"js/modules/cjsmodule.go","lineNumber":74,"sourceCode":"\nfunc (cmi *cjsModuleInstance) HasTLA() bool { return false }\n\nfunc (cmi *cjsModuleInstance) RequestedModules() []string { return cmi.w.RequestedModules() }\n\nfunc (cmi *cjsModuleInstance) ExecuteModule(\n\trt *sobek.Runtime, _, _ func(any) error,\n) (sobek.CyclicModuleInstance, error) {\n\tv, err := rt.RunProgram(cmi.w.prg)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tmodule := rt.NewObject()\n\tcmi.exports = rt.NewObject()\n\t_ = module.Set(\"exports\", cmi.exports)\n\tcall, ok := sobek.AssertFunction(v)\n\tif !ok {\n\t\tpanic(\"Somehow a CommonJS module is not wrapped in a function - \" +\n\t\t\t\"this is a k6 bug, please report it (https://github.com/grafana/k6/issues)\")\n\t}\n\tif _, err = call(cmi.exports, module, cmi.exports); err != nil {\n\t\treturn nil, err\n\t}\n\n\texportsV := module.Get(\"exports\")\n\tif sobek.IsNull(exportsV) {\n\t\treturn nil, errors.New(\"CommonJS's exports must not be null\")\n\t}\n\tcmi.exports = exportsV.ToObject(rt)\n\n\t// this will be called multiple times we only need to update this on the first VU\n\tif cmi.w.exportedNames == nil {\n\t\tcmi.w.exportedNames = cmi.exports.Keys()\n\t\tif cmi.w.exportedNames == nil { // workaround if a CommonJS module does not have exports\n\t\t\tcmi.w.exportedNames = make([]string, 0)\n\t\t}","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/js/modules/cjsmodule.go#L56-L92","documentation":"In ExecuteModule for a CommonJS module, the value returned by running the module wrapper program was not a function. Every CommonJS module is compiled into a wrapper function, so this violates an internal invariant — either the wrapping step was skipped or the program is corrupted. This is a k6 bug, not a user script error.","triggerScenarios":"Thrown at js/modules/cjsmodule.go:74 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Update to the latest k6 version","Report the issue at https://github.com/grafana/k6/issues with the script and k6 version","Check whether a custom build or extension interferes with CommonJS module compilation"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}