Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Globals and Console

Node.js Blob class

Reference for the Blob class in Node.js: build immutable binary data from strings and buffers, slice it, read it as text or bytes, and stream it.

A Blob is an immutable chunk of raw bytes with a size and an optional MIME type. You build one from an array of parts (strings, ArrayBuffers, typed arrays, Buffers or other blobs) and then read it back, slice it, or hand it to an API that accepts blobs, such as fetch() bodies, FormData and URL.createObjectURL().

Blob is a global in Node.js and follows the web standard, so code that uses it also runs in browsers. Because a blob never changes, you can share it safely; every read produces a fresh copy of the data.

NODE
const blob = new Blob(['Hello, ', 'world'], { type: 'text/plain' });
console.log(blob.size); // 12

Try it

Node.js Creating and reading a Blob

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.

Node.js Slicing and streaming

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

NODE
new Blob(sources?: Array, options?: { type?: string, endings?: 'transparent' | 'native' })

blob.size: number
blob.type: string
blob.text(): Promise<string>
blob.arrayBuffer(): Promise<ArrayBuffer>
blob.bytes(): Promise<Uint8Array>
blob.slice(start?: number, end?: number, type?: string): Blob
blob.stream(): ReadableStream
Name Description
sources Array of strings, ArrayBuffer, typed arrays, DataView, Buffer or other Blob objects. Strings are encoded as UTF-8.
options.type MIME type. It is lower-cased; a value containing characters outside printable ASCII becomes an empty string.
options.endings 'transparent' (default) keeps line endings; 'native' converts \n to the platform line ending in string parts.
start, end Byte offsets for slice(). Negative values count from the end, like Array.prototype.slice.
type (slice) MIME type of the new blob; defaults to an empty string, not to the original type.

Returns: text(), arrayBuffer() and bytes() return promises of the content; slice() returns a new Blob; stream() returns a ReadableStream.

Errors: passing a non-iterable (for example a plain string) as sources throws a TypeError with code ERR_INVALID_ARG_TYPE.

Usage notes

  • Immutable. There is no way to append to or edit a blob. Create a new one from the old blob plus the new parts.
  • slice() is cheap. It does not copy the bytes; both blobs refer to the same data. Note that slice() drops the MIME type unless you pass one.
  • Reading is asynchronous. text() decodes as UTF-8; invalid sequences become the replacement character.
  • Memory. The data is held in memory (or in a Node-managed handle). For files that may be large, prefer streams; fs.openAsBlob() returns a file-backed blob that does not load the whole file.
  • Not the same as Buffer. A Buffer is mutable and synchronous; a Blob is immutable and read through promises. Convert with Buffer.from(await blob.arrayBuffer()).
  • bytes() returns a Uint8Array directly and is available on current Node versions; on older runtimes use new Uint8Array(await blob.arrayBuffer()).
  • Passing between threads. Blobs can be posted with postMessage() to a worker without copying the bytes.
  • File. The global File class extends Blob and adds name and lastModified.