I am working with node.js and I streamed my Audio to my node.js server. Now I noticed during the process of building the audio blob:
var audioBlob = new Blob([dataview], { type: 'audio/wav' });
That I get a ReferenceError at new Blob. It seems that Blob is not supported. How can I create a blob which I would like to save with node.js fs module?
4 Answers
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;
Note: This answer only applies if you want to follow the Richie Bendall's answer.
Short answer:
Avoid importing Blob from 'node:buffer'.
Instead, prefer importing it like this (as explained in the NodeJS docs. (implementation example here)):
import { Blob } from 'buffer';