denoland/deno · error · Error

ERR_TLS_REQUIRED_SERVER_NAME

ERR_TLS_REQUIRED_SERVER_NAME

Error message

"servername" is required parameter for Server.addContext

What it means

tls.Server.prototype.addContext(servername, context) registers a SecureContext for SNI routing on the server. The polyfill requires a truthy servername; empty string, null, undefined, 0, and false all throw ERR_TLS_REQUIRED_SERVER_NAME before the [servername, context] pair is pushed onto the server's context list.

Solutions

  1. Always pass a non-empty hostname string first: server.addContext('example.com', ctx)
  2. Skip empty keys when registering from maps: if (!host) continue;
  3. For dynamic SNI prefer SNICallback, which can fall back to a default context for unknown names

Example fix

// before
for (const [host, ctx] of Object.entries(certs)) {
  server.addContext(host, ctx); // throws when host === ''
}

// after
for (const [host, ctx] of Object.entries(certs)) {
  if (host) server.addContext(host, ctx);
}
Defensive patterns

Strategy: validation

Validate before calling

for (const [host, ctx] of Object.entries(certMap)) {
  if (typeof host === 'string' && host.length > 0) {
    server.addContext(host, ctx);
  }
}

Type guard

function isNonEmptyServername(v: unknown): v is string {
  return typeof v === 'string' && v.length > 0;
}

Try / catch

try {
  server.addContext(servername, ctx);
} catch (err) {
  if ((err as NodeJS.ErrnoException).code === 'ERR_TLS_REQUIRED_SERVER_NAME') {
    // skip or log the empty-hostname entry and continue the registration loop
  } else throw err;
}

Prevention

When it happens

Trigger: server.addContext('', ctx), server.addContext(undefined, ctx), or addContext(ctx, 'example.com') with the argument order swapped so servername is a truthy-but-wrong object while the real name lands in context.

Common situations: Looping over a hostname→cert map where one key is empty; SNI registration driven by request Host headers or user input that can be blank; forgetting the parameter order.

Understand the failure class

Related errors


AI-assisted analysis of denoland/deno@f7822238ca (2026-08-20). Data as JSON: /api/errors/ed2ef0d64a451d3c. Report an issue: GitHub.

Appendix: source

Thrown at ext/node/polyfills/_tls_wrap.js:1516

    key: options.key,
    maxVersion: options.maxVersion ?? defaults?.maxVersion,
    minVersion: options.minVersion ?? defaults?.minVersion,
    passphrase: options.passphrase,
    pfx: options.pfx,
    privateKeyEngine: options.privateKeyEngine,
    privateKeyIdentifier: options.privateKeyIdentifier,
    secureOptions: options.secureOptions,
    secureProtocol: options.secureProtocol,
    sessionIdContext: options.sessionIdContext,
    sessionTimeout: options.sessionTimeout,
    sigalgs: options.sigalgs,
    ticketKeys: options.ticketKeys,
  });
};

Server.prototype.addContext = function (servername, context) {
  if (!servername) {
    throw new ERR_TLS_REQUIRED_SERVER_NAME();
  }
  ArrayPrototypePush(this._contexts, [servername, context]);
};

Server.prototype.getTicketKeys = function getTicketKeys() {
  return Buffer.from(this._ticketKeys);
};

Server.prototype.setTicketKeys = function setTicketKeys(keys) {
  if (!isArrayBufferView(keys)) {
    throw new ERR_INVALID_ARG_TYPE(
      "keys",
      ["Buffer", "TypedArray", "DataView"],
      keys,
    );
  }
  if (getViewByteLength(keys) !== 48) {
    throw new Error("Session ticket keys must be a 48-byte buffer");

View on GitHub (pinned to f7822238ca)