grafana/k6 · error

WebSocket requires a url

Error message

WebSocket requires a url

What it means

Thrown by the k6/experimental/websockets WebSocket constructor when the first argument is null, undefined, or missing. parseURL (internal/js/modules/k6/websockets/websockets.go:163) rejects nullish input with 'WebSocket requires a url' before any parsing; sibling errors cover unparsable URLs ('requires valid url') and non-ws/wss schemes.

Solutions

  1. Pass the URL string directly: new WebSocket('wss://example.com/socket')
  2. Default the env var: const url = __ENV.WS_URL || 'wss://example.com/socket'
  3. Check typeof url === 'string' && url.length > 0 before constructing, so the failure names the missing input

Example fix

// before
const url = __ENV.WS_URL; // undefined when not exported
const ws = new WebSocket(url);

// after
const url = __ENV.WS_URL || 'wss://example.com/socket';
const ws = new WebSocket(url);
Defensive patterns

Strategy: validation

Validate before calling

function requireWebSocketUrl(url) {
  if (url === null || url === undefined || url === '') {
  throw new Error(`WebSocket requires a url — got ${String(url)}. Check __ENV.WS_URL is exported.`);
  }
  return url;
}

const ws = new WebSocket(requireWebSocketUrl(__ENV.WS_URL));

Type guard

function isNonEmptyString(v) {
  return typeof v === 'string' && v.length > 0;
}

Prevention

When it happens

Trigger: new WebSocket(url) where url is undefined — e.g. const url = __ENV.WS_URL with the environment variable unset, or a renamed/misspelled variable — or explicitly new WebSocket(null). An empty string instead produces the scheme error, not this one.

Common situations: Reading the WebSocket URL from an env var that is not set in the current k6 invocation; passing an options object as the first argument by mistake; refactoring that leaves the URL variable unassigned on some path.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at internal/js/modules/k6/websockets/websockets.go:182

		connectCancel:   connectCancel,
		eventListeners:  newEventListeners(),
		obj:             rt.NewObject(),
		tagsAndMeta:     params.tagsAndMeta,
		sendPings:       ping{timestamps: make(map[string]time.Time)},
		binaryType:      blobBinaryType,
	}

	// Maybe have this after the goroutine below ?!?
	defineWebsocket(rt, w)

	go w.establishConnection(connectionCtx, params)
	return w.obj
}

// parseURL parses the url from the first constructor calls argument or returns an error
func parseURL(urlValue sobek.Value) (*url.URL, error) {
	if common.IsNullish(urlValue) {
		return nil, errors.New("WebSocket requires a url")
	}

	// TODO: throw the SyntaxError (https://websockets.spec.whatwg.org/#dom-websocket-websocket)
	urlString := urlValue.String()
	url, err := url.Parse(urlString)
	if err != nil {
		return nil, fmt.Errorf("WebSocket requires valid url, but got %q which resulted in %w", urlString, err)
	}
	if url.Scheme != "ws" && url.Scheme != "wss" {
		return nil, fmt.Errorf("WebSocket requires url with scheme ws or wss, but got %q", url.Scheme)
	}
	if url.Fragment != "" {
		return nil, fmt.Errorf("WebSocket requires no url fragment, but got %q", url.Fragment)
	}

	return url, nil
}

View on GitHub (pinned to 3fcf5388d7)