Brief summary
Unlike other network-capable modules (k6/http, k6/ws, k6/grpc), k6/websockets has no guard against being used in the init context (i.e. before any VU state exists). Calling new WebSocket(...) at the top level of a script's module body causes a nil-pointer-dereference panic instead of an error, crashing k6 inspect and k6 run with exit code 109. The deprecated alias k6/experimental/websockets is equally affected, since it wraps the same implementation.
k6 version
v2.2.0
OS
linux
Docker version and image (if applicable)
N/A
Steps to reproduce the problem
import { WebSocket } from 'k6/websockets';
new WebSocket('ws://example.invalid');
export default function () {}
$ k6 run websockets.js
time="2026-08-28T13:12:18-06:00" level=error msg="unexpected k6 panic: runtime error: invalid memory address or nil pointer dereference\ngoroutine 1 [running]:...
Expected behaviour
k6 doesn't panic
Actual behaviour
k6 panics
Brief summary
Unlike other network-capable modules (
k6/http,k6/ws,k6/grpc),k6/websocketshas no guard against being used in the init context (i.e. before any VU state exists). Callingnew WebSocket(...)at the top level of a script's module body causes a nil-pointer-dereference panic instead of an error, crashingk6 inspectandk6 runwith exit code 109. The deprecated aliask6/experimental/websocketsis equally affected, since it wraps the same implementation.k6 version
v2.2.0
OS
linux
Docker version and image (if applicable)
N/A
Steps to reproduce the problem
Expected behaviour
k6 doesn't panic
Actual behaviour
k6 panics