Node.js can't create Blobs? Node.js can't create Blobs? javascript javascript

Node.js can't create Blobs?


The Solution to this problem is to create a function which can convert between Array Buffers and Node Buffers. :)

Convert a binary NodeJS Buffer to JavaScript ArrayBuffer

In recent node versions it's just:

let buffer = Buffer.from(arraybuffer);let arraybuffer = Uint8Array.from(buffer).buffer;


Since Node.js 16, Blob can be imported:

import { Blob } from "node:buffer"new Blob([])//=> Blob {size: 0, type: ""}

Otherwise, just use cross-blob:

import Blob from "cross-blob" new Blob([])//=> Blob {size: 0, type: ""} // Global patch (to support external modules like is-blob).globalThis.Blob = Blob


Another solution to consider is to use a Base64 String to transfer data from the Server to the client.

I am working on a Node.js project where I receive audio data in the form of an ArrayBuffer, and I want to send and play that data in the browser. Most of my struggles came from trying to send the ArrayBuffer to the client or trying to convert the ArrayBuffer and send a Buffer.

What ended up being a simple solution for me was to

  1. Convert the ArrayBuffer to a Base64 encoded String on the Server
  2. Return/Send the Base64 String to the client from the server
  3. Create an Audio element/object on the client side and play the sound

I used base64-arraybuffer to perform the ArrayBuffer > Base64 String conversion (though, it may be simple to do this without a package).

I used tips from here to create the audio element on the client side.

*I haven't done much in the way of testing limits - so I don't know how this might handle large audio files.