discordjs/discord.js · error · Error

Token has not been set

Error message

Token has not been set

What it means

WebSocketManager's token getter throws if no token has been stored on the manager. The getter is used internally to hand the token to the sharding strategy, so accessing it (or triggering anything that reads it) before setToken() has been called fails.

Source

Thrown at packages/ws/src/ws/WebSocketManager.ts:262

	private shardIds: number[] | null = null;

	/**
	 * Strategy used to manage shards
	 *
	 * @defaultValue `SimpleShardingStrategy`
	 */
	private readonly strategy: IShardingStrategy;

	/**
	 * Gets the token set for this manager. If no token is set, an error is thrown.
	 * To set the token, use {@link WebSocketManager.setToken} or pass it in the options.
	 *
	 * @remarks
	 * This getter is mostly used to pass the token to the sharding strategy internally, there's not much reason to use it.
	 */
	public get token(): string {
		if (!this.#token) {
			throw new Error('Token has not been set');
		}

		return this.#token;
	}

	public constructor(options: CreateWebSocketManagerOptions) {
		if (typeof options.fetchGatewayInformation !== 'function') {
			throw new TypeError('fetchGatewayInformation is required');
		}

		super();
		this.options = {
			...DefaultWebSocketManagerOptions,
			...options,
		};
		this.strategy = this.options.buildStrategy(this);
		this.#token = options.token ?? null;
	}

View on GitHub (pinned to a81ed8a306)

Solutions

  1. Call setToken(yourToken) before accessing token or calling connect()
  2. Validate the token env var is defined and non-empty at startup
  3. If auth-free usage is intended, don't use this manager's token getter

Example fix

// before
const manager = new WebSocketManager(options);
await manager.connect(); // reads token internally -> throws
// after
const manager = new WebSocketManager(options);
manager.setToken(process.env.DISCORD_TOKEN!);
await manager.connect();
Defensive patterns

Strategy: validation

Validate before calling

const token = process.env.DISCORD_TOKEN;
if (!token || token.trim().length === 0) {
  throw new Error('DISCORD_TOKEN must be set before creating the WebSocketManager');
}
manager.setToken(token);

Type guard

function hasToken(m: WebSocketManager, tokenSet: boolean): tokenSet is true {
  return tokenSet;
}

Try / catch

try {
  const t = manager.token;
} catch {
  manager.setToken(process.env.DISCORD_TOKEN!);
}

Prevention

When it happens

Trigger: Accessing manager.token before calling setToken(); constructing a manager and connecting without ever providing a token.

Common situations: Forgetting to load the token from environment variables (e.g. DISCORD_TOKEN undefined); calling connect() before setToken() in custom sharding-strategy setups; token set on the REST instance but never on the WebSocketManager.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of discordjs/discord.js@a81ed8a306 (2026-08-30). Data as JSON: /api/errors/cf8b22b08de302dc. Report an issue: GitHub.