Hexer: Hexadecimal Dumper
Hexer is a Node.js library for generating hexadecimal dumps of buffers and streams, offering synchronous, streaming, and CLI modes. Its primary function is to transform binary data into a human-readable hex format, similar to tools like `xxd`. The package provides various transform streams for different use cases, including spying on stream data, converting entire streams, or processing data in chunks. Key customization options include control over output formatting like line prefixes, column count, grouping, and custom decorators for byte representation. The current stable version is 1.5.0, which was last published over 7 years ago, indicating it is no longer actively maintained. This means it primarily supports older CommonJS (CJS) environments and Node.js versions (engines require `>= 0.10.x`) and lacks native ES module (ESM) support or recent updates to align with modern JavaScript practices. Alternatives like `hexy` exist that are more actively maintained and support modern Node.js and ESM.
Common errors
-
TypeError: hexer is not a function
cause Attempting to use `hexer` as a default export function after incorrectly importing it using `import hex from 'hexer';` in an ESM context, or incorrectly destructuring `require('hexer')`.fixEnsure you are using `const hex = require('hexer');` for CommonJS environments. The main `hex` function is the direct export of the module, while `Spy` and `Transform` are properties on it. -
ERR_REQUIRE_ESM: Must use import to load ES Module
cause Attempting to `require('hexer')` in a Node.js environment where the current file or project is configured as an ES module (e.g., `"type": "module"` in `package.json`).fixEither convert your project or specific file to CommonJS, or use `const hex = await import('hexer').then(m => m.default || m);` for dynamic ESM import, though full compatibility isn't guaranteed. It's often better to use a modern, ESM-compatible hex dumping library.
Warnings
- breaking Hexer is a CommonJS-only package and does not provide native ES module (ESM) support. Attempting to `import` it in an ESM project will result in a 'Module not found' or similar error.
- gotcha The package has not been updated in over 7 years, and its `engines.node` field specifies `>= 0.10.x`. While it might still function on newer Node.js versions, it is not officially tested or supported for them and may encounter unexpected compatibility issues or rely on deprecated Node.js APIs.
Install
-
npm install hexer -
yarn add hexer -
pnpm add hexer
Imports
- hex
import hex from 'hexer';
const hex = require('hexer'); - hex.Spy
import { Spy } from 'hexer';const hex = require('hexer'); // ... hex.Spy(process.stdout); - hex.Transform
import { Transform } from 'hexer';const hex = require('hexer'); // ... hex.Transform();
Quickstart
const hex = require('hexer');
const { Readable, Writable } = require('stream');
// Example 1: Buffer to string
const someBuffer = Buffer.from('Hello, Hexer!');
console.log('Buffer dump:\n' + hex(someBuffer));
// Example 2: Streaming transform
const inputData = Buffer.from('This is a test stream with some data.\nAnother line here.');
const readableStream = new Readable({ read() {} });
console.log('\nStreaming transform:');
readableStream
.pipe(hex.Transform({ cols: 8, group: 4, headSep: '| ' }))
.pipe(new Writable({
write(chunk, encoding, callback) {
process.stdout.write(chunk.toString());
callback();
}
}));
readableStream.push(inputData.slice(0, 20));
readableStream.push(inputData.slice(20, 40));
readableStream.push(null); // End the stream