Borislav Hadzhiev
Last updated: Jul 25, 2022
Check out my new book
The "Blob is not defined" error occurs when the Blob
class is used without
being imported in a Node.js application. To solve the error import the Blob
class before using it, e.g. import { Blob } from 'buffer';
.
To solve the error, import the Blob class before using it.
import {Blob} from 'buffer'; const blob = new Blob(['hello world']); console.log(blob); // 👉️ Blob { size: 11, type: '' }
The Blob
class takes 2 parameters:
Blob
endings
to transparent
or
native
(line endings get converted to the platform native line endings).
The content-type of the Blob
can also be set in the object.Blob
constructor creates and returns a new Blob
object that contains the concatenation of the provided sources.You can modify the sources after the Blob
is created.
Here's an example were we explicitly set the content-type of the Blob
.
import {Blob} from 'buffer'; const obj = {name: 'James Doe'}; const blob = new Blob([JSON.stringify(obj, null, 2)], { type: 'application/json', }); console.log(blob); // 👉️ Blob { size: 25, type: 'application/json' }
We constructed a Blob
from a JSON string and set its content-type to
application/json
.