{"id":12919,"library":"buffer","title":"Node.js Buffer API for the Browser","description":"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.","status":"active","version":"6.0.3","language":"javascript","source_language":"en","source_url":"git://github.com/feross/buffer","tags":["javascript","arraybuffer","browser","browserify","buffer","compatible","dataview","uint8array","typescript"],"install":[{"cmd":"npm install buffer","lang":"bash","label":"npm"},{"cmd":"yarn add buffer","lang":"bash","label":"yarn"},{"cmd":"pnpm add buffer","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const Buffer = require('buffer');","symbol":"Buffer","correct":"import { Buffer } from 'buffer';"},{"note":"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.","wrong":"var Buffer = require('buffer');","symbol":"Buffer","correct":"var Buffer = require('buffer/').Buffer;"},{"note":"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.","symbol":"Buffer (Global)","correct":"Buffer.from('hello');"},{"note":"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.","symbol":"Buffer type","correct":"import type { Buffer } from 'buffer';"}],"quickstart":{"code":"import { Buffer } from 'buffer'; // Explicit import for clarity and bundler compatibility\n\n// Create a buffer from a string\nconst buf1 = Buffer.from('hello world', 'utf8');\nconsole.log('Buffer 1 (string):', buf1.toString());\n\n// Create a buffer from an array of octets (byte values)\nconst buf2 = Buffer.from([0x48, 0x65, 0x6c, 0x6c, 0x6f]); // 'Hello'\nconsole.log('Buffer 2 (array):', buf2.toString('utf8'));\n\n// Allocate a buffer of 10 bytes and write into it\nconst buf3 = Buffer.alloc(10);\nbuf3.write('test', 0, 4); // Write 'test' at offset 0, up to 4 bytes\nconsole.log('Buffer 3 (written part):', buf3.toString('utf8', 0, 4));\nconsole.log('Buffer 3 (full, remaining empty):', buf3.toString()); // Shows 'test\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000'\n\n// Concatenate multiple buffers\nconst concatenatedBuffer = Buffer.concat([buf1, buf2]);\nconsole.log('Concatenated Buffer:', concatenatedBuffer.toString());\n\n// Check if an object is an instance of Buffer\nconsole.log('Is buf1 an instance of Buffer?', Buffer.isBuffer(buf1));\nconsole.log('Is a plain object an instance of Buffer?', Buffer.isBuffer({}));\n","lang":"typescript","description":"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."},"warnings":[{"fix":"Run `npm uninstall native-buffer-browserify` and then `npm install buffer`. Update all import/require statements from `native-buffer-browserify` to `buffer`.","message":"The 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.","severity":"breaking","affected_versions":"<6.0.0"},{"fix":"Always use `require('buffer/').Buffer` to explicitly reference the npm package's implementation, ensuring you get the browser-compatible polyfill.","message":"When 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.","severity":"gotcha","affected_versions":">=0.1.0"},{"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.","message":"This 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.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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';`.","cause":"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.","error":"ReferenceError: Buffer is not defined"},{"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.","cause":"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.","error":"TypeError: Buffer.from is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}