Node.js can't create Blobs?
In Node.js, think Buffer when you need to work with binary data:
Got some code expecting a Blob? Use the buffer library as a stand-in:
In the Node.js universe, Buffer reigns supreme. Using Blob from the buffer library helps when your code needs to impersonate browser code.
Blob emulation in pre-Node.js 16
Starting with Node.js 16, we received the Blob support natively. Great news, indeed! However, we still have to sympathize with those stuck in older versions. For them, third-party libraries to the rescue!
Creating a Blob just like we do in the browser, no big deal with cross-blob. Hang on, browser... we're catching up!
Binary data handling with Buffer and fs
Remember, Buffer is Node.js**'s native way of dealing with **binary data**. When it comes to files or streamed data (like **WAV** audio), **Buffer** and the **fs` module go together like bread and butter:
It's that simple! A Willy Wonka golden ticket to Binaryville.
Unit testing victory with Blob
In the trenches of unit testing with Jest, a Blob can feel like a slippery eel. No worries though, we've got the right gear to handle it:
Clipboard between Node.js and browser
If you're working on code meant for both browsers and Node.js, you can't ignore Blob. Thankfully there are polyfills that let Blob feel right at home even in Node.js.
Just like that, we can confidently talk about Blob in Node.js and browser alike!
And finally, end your Blob creation by setting the correct MIME type.
Was this article helpful?