Node.js Buffer for Browsers (ES6 Rewrite)
buffer-es6 provides a robust and performant implementation of the Node.js Buffer API, specifically re-engineered using ES6 syntax for browser environments. It is optimized for bundlers like Rollup, enabling better tree-shaking compared to its predecessor. The current stable version is 4.9.3. While no explicit release cadence is provided, the package appears actively maintained as a specialized fork of the main `buffer` package. Its key differentiators include identical API compatibility with Node.js Buffer, broad browser support (down to IE6), and a remarkably small bundle size (5.04KB minified + gzipped). It internally bundles `ieee754` and `base64-js`, providing a self-contained solution for binary data manipulation in the browser.
Common errors
-
TypeError: Buffer is not a constructor
cause Attempting to use `Buffer` as a constructor or class when it was imported incorrectly, often via a default import instead of a named export (ESM) or by destructuring `require('buffer-es6')` (CJS).fixFor ESM, use `import { Buffer } from 'buffer-es6';`. For CJS, use `const Buffer = require('buffer-es6');`. -
Cannot find module 'buffer-es6'
cause The `buffer-es6` package has not been installed, or the import path is incorrect.fixRun `npm install buffer-es6` or `yarn add buffer-es6`. Ensure the import path is exactly `'buffer-es6'` for both ESM and CJS. -
Cannot find module 'buffer'
cause Attempting to import the core `buffer` module or the `buffer` npm package when `buffer-es6` is installed and the environment expects the specific `buffer-es6` module. This often happens due to confusion between the two packages.fixIf intending to use `buffer-es6`, ensure all your import paths reference `'buffer-es6'`. If you truly need the main `buffer` npm package (which provides Node.js core-like Buffer behavior), install it with `npm install buffer` and adjust imports accordingly (e.g., `require('buffer/').Buffer` for CJS).
Warnings
- breaking In older browsers lacking Typed Array support (e.g., IE6-9), `buf.slice()` returns a new Buffer that does NOT share underlying memory with the original. Modifications to one will not affect the other, unlike Node.js behavior and modern browser implementations.
- gotcha The `buffer-es6` package is a distinct npm package from the more commonly known `buffer` package. They have different installation commands (`npm install buffer-es6` vs `npm install buffer`) and different primary import paths. Confusing the two can lead to module not found errors or unexpected behavior.
- gotcha `buffer-es6` explicitly disables certain Node.js compatibility features present in the core `buffer` package. This was done to facilitate Rollup tree-shaking. While generally beneficial for bundle size, it might remove specific behaviors or polyfills expected in highly specialized or legacy contexts.
- gotcha The bundled `base64-js` dependency, as mentioned in the README, has an 'MIT with no author listed' license attribution. While MIT is permissive, the lack of an author might be a concern for projects with strict license compliance requirements.
Install
-
npm install buffer-es6 -
yarn add buffer-es6 -
pnpm add buffer-es6
Imports
- Buffer
import Buffer from 'buffer-es6';
import { Buffer } from 'buffer-es6'; - Buffer
const { Buffer } = require('buffer-es6');const Buffer = require('buffer-es6'); - Buffer (alternative core Buffer package import)
import { Buffer } from 'buffer';const Buffer = require('buffer/').Buffer;
Quickstart
import { Buffer } from 'buffer-es6';
// Create a new Buffer of 10 bytes and fill it with zeros
const buf1 = Buffer.alloc(10);
console.log('Buffer 1 (alloc):', buf1.toString('hex'));
// Create a Buffer from a string, using UTF-8 encoding
const buf2 = Buffer.from('Hello Buffer!', 'utf8');
console.log('Buffer 2 (from string):', buf2.toString());
// Write data to an existing buffer
buf1.write('ABC', 0, 3, 'utf8');
console.log('Buffer 1 after write:', buf1.toString('utf8'));
// Concatenate two buffers
const buf3 = Buffer.concat([buf1, buf2]);
console.log('Buffer 3 (concat):', buf3.toString('utf8'));
// Check buffer length
console.log('Buffer 3 length:', buf3.length);
// Access individual bytes
console.log('Byte at index 1 of buf2:', buf2[1]); // Prints 'e' ASCII value
// Convert to JSON
const json = buf2.toJSON();
console.log('Buffer 2 as JSON:', json);
// Create a Buffer from a JavaScript ArrayBuffer
const arrayBuffer = new ArrayBuffer(5);
const uint8Array = new Uint8Array(arrayBuffer);
uint8Array[0] = 72; // 'H'
uint8Array[1] = 105; // 'i'
const buf4 = Buffer.from(arrayBuffer);
console.log('Buffer 4 (from ArrayBuffer):', buf4.toString());
// Check if a variable is a Buffer
console.log('Is buf1 a Buffer?', Buffer.isBuffer(buf1));
console.log('Is "Hello" a Buffer?', Buffer.isBuffer('Hello'));