How to use 'zlib constants' in Node.js
Zlib is a built-in module in Node.js that provides decompression or compression properties on strings and buffers in a program.
The zlib.constants include dozens of operations that we do not need to include separately in our program. Those operations include:
- Flush values
- Return codes for compression and decompression functions
- The deflate compression method
- Initializing
zalloc,zfree, andopaque - Compression strategy
- Compression levels
These constant operations are pre-loaded every time we include the zlib.constant in our program.
Syntax
As stated above, we do not need to define the constants by ourselves. The basic syntax of constant operations is as follows:
#define Z_NO_FLUSH
#define Z_STREAM_END
#define Z_NO_COMPRESSION
#define Z_BINARY
Code
For better understanding, observe the following code illustration.
const zlib = require("zlib");console.log("Values for constants:")console.log("Z_NO_FLUSH: ", zlib.constants.Z_NO_FLUSH)console.log("Z_STREAM_END: ", zlib.constants.Z_STREAM_END)console.log("Z_MEM_ERROR: ", zlib.constants.Z_MEM_ERROR)console.log("Z_BUF_ERROR: ", zlib.constants.Z_BUF_ERROR)console.log("Z_NO_COMPRESSION: ", zlib.constants.Z_NO_COMPRESSION)console.log("Z_DEFAULT_STRATEGY: ", zlib.constants.Z_DEFAULT_STRATEGY)
Explanation
The above code prints the levels for constants used in the example. zlib module is imported in the program by typing const zlib = require("zlib");.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved