{"record":{"id":"b695ad71bc03b628","repo":"discordjs/discord.js","slug":"websocketshard-compression-is-set-to-native-zlib","errorCode":null,"errorMessage":"WebSocketShard: Compression is set to native zlib but node:zlib is not available.","messagePattern":"WebSocketShard: Compression is set to native zlib but node:zlib is not available\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"packages/ws/src/ws/WebSocketShard.ts","lineNumber":219,"sourceCode":"\t\t\t\t\tif (zlib) {\n\t\t\t\t\t\tthis.inflateBuffer = [];\n\n\t\t\t\t\t\tconst inflate = zlib.createInflate({\n\t\t\t\t\t\t\tchunkSize: 65_535,\n\t\t\t\t\t\t\tflush: zlib.constants.Z_SYNC_FLUSH,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tinflate.on('data', (chunk) => {\n\t\t\t\t\t\t\tthis.inflateBuffer.push(chunk);\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tinflate.on('error', (error) => {\n\t\t\t\t\t\t\tthis.emit(WebSocketShardEvents.Error, error);\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tthis.nativeInflate = inflate;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tconsole.warn('WebSocketShard: Compression is set to native zlib but node:zlib is not available.');\n\t\t\t\t\t\tparams.delete('compress');\n\t\t\t\t\t}\n\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tcase CompressionMethod.ZlibSync: {\n\t\t\t\t\tconst zlib = await getZlibSync();\n\t\t\t\t\tif (zlib) {\n\t\t\t\t\t\tthis.zLibSyncInflate = new zlib.Inflate({\n\t\t\t\t\t\t\tchunkSize: 65_535,\n\t\t\t\t\t\t\tto: 'string',\n\t\t\t\t\t\t});\n\t\t\t\t\t} else {\n\t\t\t\t\t\tconsole.warn('WebSocketShard: Compression is set to zlib-sync, but it is not installed.');\n\t\t\t\t\t\tparams.delete('compress');\n\t\t\t\t\t}\n","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/discordjs/discord.js/blob/a81ed8a306d37fdc746e26a634b6a42164ba2c8c/packages/ws/src/ws/WebSocketShard.ts#L201-L237","documentation":"Warning from WebSocketShard.internalConnect when compression is configured as native zlib (node:zlib-based inflate) but getNativeZlib() returned null — meaning the node:zlib module is unavailable. The shard deletes the 'compress' URL parameter so the gateway sends uncompressed payloads. This only occurs when a compression library/feature was requested but cannot be satisfied in the current runtime.","triggerScenarios":"Setting WebSocketShardOptions.compression to the native zlib method in an environment where node:zlib cannot be imported (e.g. custom/minimal Node builds compiled without zlib support, edge/serverless runtimes that stub node builtins, bundlers replacing node:zlib with a failing shim).","commonSituations":"Running discord.js on serverless/edge platforms (Vercel Edge, Cloudflare Workers with node compatibility shims); custom-compiled Node.js without zlib; bundling (webpack/esbuild) misconfiguring node builtins as externals/shims.","solutions":["Use a full Node.js build with zlib support (official Node distributions include node:zlib).","Install and configure zlib-sync instead: npm i zlib-sync and set compression to the zlib-sync method.","Set compression: null to explicitly opt out of gateway compression.","Fix bundler config so node:zlib is external (e.g. 'node-builtins' external in esbuild/webpack), or run in a Node runtime instead of an edge runtime."],"exampleFix":"// before (edge runtime)\nexport const runtime = 'edge';\n// after\nexport const runtime = 'nodejs'; // node:zlib available","handlingStrategy":"validation","validationCode":"let nativeZlibAvailable = false;\ntry { await import('node:zlib'); nativeZlibAvailable = true; } catch {}\nif (compression === 'zlib-native' && !nativeZlibAvailable) {\n  compression = null; // or fall back to zlib-sync\n}","typeGuard":"function hasNativeZlib(z) { return z != null && typeof z.createInflate === 'function'; }","tryCatchPattern":"try {\n  await import('node:zlib');\n} catch {\n  // runtime lacks node:zlib; disable compression before connecting\n  shardOptions.compression = null;\n}","preventionTips":["Deploy on official Node.js builds, not minimal/custom compiles without zlib.","In edge/serverless environments, verify node builtin support before enabling compression.","Check the warn log at startup and fall back to zlib-sync or compression: null."],"tags":["websocket","compression","nodejs","zlib","runtime-environment"],"backgroundTag":"native-module-unavailable","analyzedSha":"a81ed8a306d37fdc746e26a634b6a42164ba2c8c","analyzedAt":"2026-08-30T04:07:22.193Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}