grafana/k6 · error

invalid GRPC Stream's client: %w

Error message

invalid GRPC Stream's client: %w

What it means

The first argument of new grpc.Stream(client, methodName, params) must be a k6 gRPC Client instance. stream() passes it to extractClient(), which unwraps the JS value and type-asserts the native Go *Client; undefined, null, the grpc.Client class itself, or a look-alike object from another module fails the assertion and the constructor throws immediately.

Source

Thrown at internal/js/modules/k6/grpc/grpc.go:117

	mustAddServingStatus("HealthCheckServing", healthpb.HealthCheckResponse_SERVING)
	mustAddServingStatus("HealthCheckNotServing", healthpb.HealthCheckResponse_NOT_SERVING)
	mustAddServingStatus("HealthCheckServiceUnkown", healthpb.HealthCheckResponse_SERVICE_UNKNOWN)
}

// Exports returns the exports of the grpc module.
func (mi *ModuleInstance) Exports() modules.Exports {
	return modules.Exports{
		Named: mi.exports,
	}
}

// stream returns a new stream object
func (mi *ModuleInstance) stream(c sobek.ConstructorCall) *sobek.Object {
	rt := mi.vu.Runtime()

	client, err := extractClient(c.Argument(0), rt)
	if err != nil {
		common.Throw(rt, fmt.Errorf("invalid GRPC Stream's client: %w", err))
	}

	methodName := sanitizeMethodName(c.Argument(1).String())
	methodDescriptor, err := client.getMethodDescriptor(methodName)
	if err != nil {
		common.Throw(rt, fmt.Errorf("invalid GRPC Stream's method: %w", err))
	}

	p, err := newCallParams(mi.vu, c.Argument(2))
	if err != nil {
		common.Throw(rt, fmt.Errorf("invalid GRPC Stream's parameters: %w", err))
	}

	p.SetSystemTags(mi.vu.State(), client.addr, methodName)

	logger := mi.vu.State().Logger.WithField("streamMethod", methodName)

	s := &stream{

View on GitHub (pinned to 93accf6570)

Solutions

  1. Create the client first and pass that instance: const client = new grpc.Client(); ...; new grpc.Stream(client, ...)
  2. Check the variable you pass is the same one you called load()/connect() on and is in scope at the call site
  3. Do not wrap the client in another object; Stream needs the raw k6/grpc Client

Example fix

// before
const stream = new grpc.Stream(client, '/pkg.Svc/Method'); // client is undefined here

// after
const client = new grpc.Client();
client.load([], 'service.proto');
client.connect('localhost:8080');
const stream = new grpc.Stream(client, '/pkg.Svc/Method');
Defensive patterns

Strategy: type-guard

Validate before calling

if (!isGrpcClient(client)) {
  throw new Error(`grpc.Stream requires a k6 grpc.Client instance, got ${client}`);
}
const stream = new grpc.Stream(client, METHOD);

Type guard

function isGrpcClient(v) {
  return v != null && typeof v === 'object' && v instanceof grpc.Client;
}

Prevention

When it happens

Trigger: new grpc.Stream(undefined, ...) or calling Stream before any client was created; passing the class instead of an instance (new grpc.Stream(grpc.Client, ...)); passing an object that merely wraps the real client; passing a client from k6/http or another module.

Common situations: Refactors that moved client creation into a helper which returns undefined on an error path; copy-pasting a Stream example while the local variable has a different name; the client variable being shadowed in an inner scope.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/718d0e00b619dc7d. Report an issue: GitHub.