{"id":15973,"library":"buffer-es6","title":"Node.js Buffer for Browsers (ES6 Rewrite)","description":"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.","status":"active","version":"4.9.3","language":"javascript","source_language":"en","source_url":"git://github.com/calvinmetcalf/buffer-es6","tags":["javascript","arraybuffer","browser","browserify","buffer","compatible","dataview","uint8array"],"install":[{"cmd":"npm install buffer-es6","lang":"bash","label":"npm"},{"cmd":"yarn add buffer-es6","lang":"bash","label":"yarn"},{"cmd":"pnpm add buffer-es6","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For ES Modules, `Buffer` is a named export. Attempting a default import will result in a TypeError.","wrong":"import Buffer from 'buffer-es6';","symbol":"Buffer","correct":"import { Buffer } from 'buffer-es6';"},{"note":"For CommonJS, the module directly exports the `Buffer` constructor. Destructuring `require('buffer-es6')` is incorrect.","wrong":"const { Buffer } = require('buffer-es6');","symbol":"Buffer","correct":"const Buffer = require('buffer-es6');"},{"note":"This specific import path (`require('buffer/')`) is for the *main* `buffer` npm package, not `buffer-es6`. It is critical to distinguish between `buffer-es6` and the core `buffer` package, as they have different installation and import conventions. Confusion between the two is a common pitfall.","wrong":"import { Buffer } from 'buffer';","symbol":"Buffer (alternative core Buffer package import)","correct":"const Buffer = require('buffer/').Buffer;"}],"quickstart":{"code":"import { Buffer } from 'buffer-es6';\n\n// Create a new Buffer of 10 bytes and fill it with zeros\nconst buf1 = Buffer.alloc(10);\nconsole.log('Buffer 1 (alloc):', buf1.toString('hex'));\n\n// Create a Buffer from a string, using UTF-8 encoding\nconst buf2 = Buffer.from('Hello Buffer!', 'utf8');\nconsole.log('Buffer 2 (from string):', buf2.toString());\n\n// Write data to an existing buffer\nbuf1.write('ABC', 0, 3, 'utf8');\nconsole.log('Buffer 1 after write:', buf1.toString('utf8'));\n\n// Concatenate two buffers\nconst buf3 = Buffer.concat([buf1, buf2]);\nconsole.log('Buffer 3 (concat):', buf3.toString('utf8'));\n\n// Check buffer length\nconsole.log('Buffer 3 length:', buf3.length);\n\n// Access individual bytes\nconsole.log('Byte at index 1 of buf2:', buf2[1]); // Prints 'e' ASCII value\n\n// Convert to JSON\nconst json = buf2.toJSON();\nconsole.log('Buffer 2 as JSON:', json);\n\n// Create a Buffer from a JavaScript ArrayBuffer\nconst arrayBuffer = new ArrayBuffer(5);\nconst uint8Array = new Uint8Array(arrayBuffer);\nuint8Array[0] = 72; // 'H'\nuint8Array[1] = 105; // 'i'\nconst buf4 = Buffer.from(arrayBuffer);\nconsole.log('Buffer 4 (from ArrayBuffer):', buf4.toString());\n\n// Check if a variable is a Buffer\nconsole.log('Is buf1 a Buffer?', Buffer.isBuffer(buf1));\nconsole.log('Is \"Hello\" a Buffer?', Buffer.isBuffer('Hello'));","lang":"typescript","description":"Demonstrates various fundamental Buffer operations including creation, writing, reading, concatenation, and type checking, using ES module syntax."},"warnings":[{"fix":"For critical applications in very old browser environments, avoid relying on shared memory behavior of `slice()` or implement custom copying logic. This typically does not affect modern browsers.","message":"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.","severity":"breaking","affected_versions":"<=4.9.3"},{"fix":"Always ensure you install `buffer-es6` if that is the intended package, and use `import { Buffer } from 'buffer-es6'` (ESM) or `const Buffer = require('buffer-es6')` (CJS) for correct imports.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Review the specific 'compatibility stuff' turned off by inspecting the `buffer-es6` source or consulting its documentation if you encounter unexpected behavior related to Buffer operations, especially when migrating from the standard `buffer` package.","message":"`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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If this is a legal concern, review the license information of `base64-js` within the `buffer-es6` package or consult legal counsel. Alternatively, consider using a different Buffer implementation if this specific licensing detail is problematic.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"For ESM, use `import { Buffer } from 'buffer-es6';`. For CJS, use `const Buffer = require('buffer-es6');`.","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).","error":"TypeError: Buffer is not a constructor"},{"fix":"Run `npm install buffer-es6` or `yarn add buffer-es6`. Ensure the import path is exactly `'buffer-es6'` for both ESM and CJS.","cause":"The `buffer-es6` package has not been installed, or the import path is incorrect.","error":"Cannot find module 'buffer-es6'"},{"fix":"If 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).","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.","error":"Cannot find module 'buffer'"}],"ecosystem":"npm"}