Node.js Reference ยท Buffers and Encoding
Node.js Buffer.from() method
Reference for Buffer.from() in Node.js: create buffers from strings with an encoding, arrays, ArrayBuffers and other buffers, copy versus shared memory, and errors.
Buffer.from() is the general-purpose way to create a Buffer from existing data. It has several forms: a string plus an encoding, an array of byte values, an array-like object, another buffer or typed array (copied), or an ArrayBuffer (viewed, not copied). It replaces the deprecated new Buffer() constructor.
The result depends on the argument. A string is encoded to bytes, an array becomes one byte per element, a Buffer or typed array is copied byte by byte, and an ArrayBuffer becomes a window on the same memory. Knowing which of those copies and which shares is the main thing to remember.
const fromText = Buffer.from('Hi', 'utf8'); const fromBytes = Buffer.from([72, 105]); console.log(fromText.equals(fromBytes)); // true
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
Buffer.from(string[, encoding]) Buffer.from(array) Buffer.from(buffer) Buffer.from(arrayBuffer[, byteOffset[, length]]) Buffer.from(object[, offsetOrEncoding[, length]])
| Name | Description |
|---|---|
string |
Text to encode. |
encoding |
Encoding of string: 'utf8' (default), 'hex', 'base64', 'base64url', 'latin1', 'ascii', 'utf16le', 'ucs2'. |
array |
Array or array-like of numbers. Each value is reduced modulo 256. |
buffer |
A Buffer or Uint8Array/typed array; its contents are copied. |
arrayBuffer |
An ArrayBuffer or SharedArrayBuffer; shared, not copied. |
byteOffset, length |
Which part of the ArrayBuffer to expose. |
object |
An object with valueOf() or Symbol.toPrimitive that returns one of the above. |
Returns: a new Buffer.
Errors: ERR_INVALID_ARG_TYPE for numbers, null and other unsupported types, ERR_UNKNOWN_ENCODING for an unknown encoding name, ERR_BUFFER_OUT_OF_BOUNDS when byteOffset or length is outside the ArrayBuffer.
Usage notes
- Use
Buffer.alloc(n)when you want a buffer of a given size;Buffer.from(5)is an error, not a five-byte buffer. - Hex and base64 decoding is forgiving: it stops at the first invalid hex digit and silently returns what it decoded so far, so
Buffer.from('zz41', 'hex')is empty. A hex string with an odd length drops the last digit. Validate input if it matters. - Encodings that cannot represent a character replace it:
'latin1'and'ascii'keep only the low byte, so'โฌ'becomes a single byte. Use UTF-8 unless a format says otherwise. Buffer.from(typedArray)copies the values, truncating each to a byte, which is rarely what you want for 16-bit data. To reinterpret the bytes, passtypedArray.bufferwith the right offset and length.Buffer.from(buffer)always copies. To avoid a copy, usebuf.subarray().- Small results may be taken from a shared internal pool, which is why
buf.buffercan be larger than the buffer. UseBuffer.from(buf.buffer, buf.byteOffset, buf.length)when passing memory to APIs that need an exactArrayBuffer. JSON.parse()output ofbuf.toJSON()({ type: 'Buffer', data: [...] }) can be revived withBuffer.from(obj.data).