{"record":{"id":"eeb6e93b8485ec67","repo":"grafana/k6","slug":"the-listener-argument-must-be-a-function","errorCode":null,"errorMessage":"The \"listener\" argument must be a function","messagePattern":"The \"listener\" argument must be a function","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/browser/page_mapping.go","lineNumber":777,"sourceCode":"\t\t\t}\n\t\t\tvar mehs []mapping\n\t\t\tfor _, eh := range ehs {\n\t\t\t\tehm := mapElementHandle(vu, eh)\n\t\t\t\tmehs = append(mehs, ehm)\n\t\t\t}\n\t\t\treturn mehs, nil\n\t\t})\n\t}\n\n\treturn maps\n}\n\n// mapPageOn enables using various page.on event handlers with the page.on method.\n// It provides a generic way to map different event types to their respective handler functions.\nfunc mapPageOn(vu moduleVU, p *common.Page) func(common.PageEventName, sobek.Callable) error {\n\treturn func(eventName common.PageEventName, handle sobek.Callable) error {\n\t\tif handle == nil {\n\t\t\tpanic(vu.Runtime().NewTypeError(`The \"listener\" argument must be a function`))\n\t\t}\n\n\t\tpageEvents := map[common.PageEventName]struct {\n\t\t\tmapp func(vu moduleVU, event common.PageEvent) mapping\n\t\t\twait bool // Whether to wait for the handler to complete.\n\t\t}{\n\t\t\tcommon.PageEventConsole:         {mapp: mapConsoleMessage},\n\t\t\tcommon.PageEventMetric:          {mapp: mapMetricEvent, wait: true},\n\t\t\tcommon.PageEventRequest:         {mapp: mapRequestEvent},\n\t\t\tcommon.PageEventResponse:        {mapp: mapResponseEvent},\n\t\t\tcommon.PageEventRequestFinished: {mapp: mapRequestEvent},\n\t\t\tcommon.PageEventRequestFailed:   {mapp: mapRequestEvent},\n\t\t}\n\t\tpageEvent, ok := pageEvents[eventName]\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"unknown page on event: %q\", eventName)\n\t\t}\n","sourceCodeStart":759,"sourceCodeEnd":795,"githubUrl":"https://github.com/grafana/k6/blob/8d06114777191d21ef885d05c5f8aa9e1ed838ae/internal/js/modules/k6/browser/browser/page_mapping.go#L759-L795","documentation":"mapPageOn rejects a page.on(...) call whose second argument is not callable: the sobek.Callable received is nil, so the listener passed from JavaScript is missing or not a function. k6 raises a TypeError to the JS runtime mirroring the browser API's behavior.","triggerScenarios":"Thrown at internal/js/modules/k6/browser/browser/page_mapping.go:776 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Pass a function as the listener: page.on('console', (msg) => { ... })","Check the event name is one of the supported page events","Make sure the callback isn't accidentally the result of calling the function instead of the function itself"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"8d06114777191d21ef885d05c5f8aa9e1ed838ae","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}