Registry / http-networking / buffer

buffer

JSON →
library0.2.1jsnpmunverified

The `buffer` package provides a robust and faithful polyfill for Node.js's `Buffer` API, allowing developers to manipulate binary data directly within web browsers. Currently at version 6.0.3, this library is actively maintained and aims for near 100% API compatibility with Node.js's native `Buffer` module, including its latest (unstable) API features, while acknowledging the underlying `Buffer` API itself is stable. It differentiates itself by leveraging modern JavaScript `Uint8Array` and `ArrayBuffer` for high performance and efficient memory use, resulting in an extremely small bundle size (6.75KB minified + gzipped). This approach ensures broad browser compatibility across Chrome, Firefox, Edge, Safari 11+, iOS 11+, and Android. The package is a crucial component for enabling many Node.js modules to function seamlessly in browser environments, often used in conjunction with bundlers like Browserify which automatically substitute Node's core `buffer` module with this browser-compatible version, providing a consistent binary data handling experience.

npm install buffer
INSTALL
IMPORT
SIG · BUFFER
B
buffer
http-networkingjavascriptv0.2.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Buffer
import { Buffer } from 'buffer';
const Buffer = require('buffer');
While CommonJS `require('buffer/')` is the explicit way to get the npm package, ESM environments should generally use named imports. However, be aware of bundler configurations as this package primarily polyfills the global `Buffer` in browser environments for CommonJS.
Buffer
var Buffer = require('buffer/').Buffer;
var Buffer = require('buffer');
For CommonJS environments, the trailing slash in `require('buffer/')` is critical. It signals to Node.js's module resolution (and many bundlers) to load the npm package named 'buffer' instead of Node.js's built-in core 'buffer' module. Omitting the slash will often result in the native Node.js Buffer being loaded, which may not be compatible with browser environments.
Buffer (Global)
Buffer.from('hello');
When using bundlers like Browserify, this package is often configured to automatically replace Node.js's core 'buffer' module, making the `Buffer` global available without an explicit `require` or `import` statement in the browser context.
Buffer type
import type { Buffer } from 'buffer';
For TypeScript, explicitly importing the `Buffer` type is recommended to ensure correct type checking, especially if you are relying on the global `Buffer` at runtime or if your bundler setup has specific aliasing.

This quickstart demonstrates common `Buffer` operations: creating from strings and arrays, allocating and writing data, concatenating buffers, and type checking, using the browser-compatible Node.js Buffer API.

import { Buffer } from 'buffer'; // Explicit import for clarity and bundler compatibility // Create a buffer from a string const buf1 = Buffer.from('hello world', 'utf8'); console.log('Buffer 1 (string):', buf1.toString()); // Create a buffer from an array of octets (byte values) const buf2 = Buffer.from([0x48, 0x65, 0x6c, 0x6c, 0x6f]); // 'Hello' console.log('Buffer 2 (array):', buf2.toString('utf8')); // Allocate a buffer of 10 bytes and write into it const buf3 = Buffer.alloc(10); buf3.write('test', 0, 4); // Write 'test' at offset 0, up to 4 bytes console.log('Buffer 3 (written part):', buf3.toString('utf8', 0, 4)); console.log('Buffer 3 (full, remaining empty):', buf3.toString()); // Shows 'test\u0000\u0000\u0000\u0000\u0000\u0000' // Concatenate multiple buffers const concatenatedBuffer = Buffer.concat([buf1, buf2]); console.log('Concatenated Buffer:', concatenatedBuffer.toString()); // Check if an object is an instance of Buffer console.log('Is buf1 an instance of Buffer?', Buffer.isBuffer(buf1)); console.log('Is a plain object an instance of Buffer?', Buffer.isBuffer({}));
Debug
Known issues
breakingThe package was previously published under the name `native-buffer-browserify`. Users upgrading from older setups or migrating projects must update their `package.json` and any direct `require` statements to use `buffer` instead.
fix
Run `npm uninstall native-buffer-browserify` and then `npm install buffer`. Update all import/require statements from `native-buffer-browserify` to `buffer`.
affects: <6.0.0
gotchaWhen explicitly requiring the `buffer` package in CommonJS environments (especially without a bundler like Browserify), using `require('buffer')` (without the trailing slash) may resolve to Node.js's built-in core `buffer` module instead of this npm package, leading to unexpected behavior in browser contexts or type inconsistencies.
fix
Always use `require('buffer/').Buffer` to explicitly reference the npm package's implementation, ensuring you get the browser-compatible polyfill.
affects: >=0.1.0
gotchaThis module aims to track the Buffer API in the latest (unstable) version of Node.js. While the Node.js Buffer API itself is considered stable, direct adherence to the 'unstable' branch implies potential for minor API changes or additions before they stabilize, though actual breaking changes are rare for Buffer.
fix
Monitor release notes for the `buffer` package and Node.js Buffer API documentation to stay informed of any changes, especially if pinning to specific Node.js versions.
affects: >=0.1.0
Errors
Common errors & fixes
ReferenceError: Buffer is not defined
The global `Buffer` object is not available in a browser environment by default, and the module was not explicitly imported or configured by a bundler.
fix
If using a bundler like Browserify, ensure it's properly configured to alias Node's `buffer` to this package. If not using a bundler or for explicit usage, import it: `var Buffer = require('buffer/').Buffer;` or `import { Buffer } from 'buffer';`.
TypeError: Buffer.from is not a function
The `Buffer` object in scope is either not the `buffer` npm package's implementation or is an incompatible version. This often happens if Node.js's core `buffer` module is inadvertently loaded in a browser context, or if there's a conflict with other polyfills.
fix
Ensure you are explicitly importing the `buffer` npm package using `require('buffer/').Buffer` (for CJS) or `import { Buffer } from 'buffer';` (for ESM) to guarantee you are using the browser-compatible polyfill.
Upgrade
Version history
0.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
21 hits · last 30 days
node
18
Amazon
1
OpenAI (training)
1
Resources
buffer — npm install buffer · libregistry