Skip to main content

How to Resolve the "ReferenceError: Blob is not defined" Error in Node.js in JavaScript

The ReferenceError: Blob is not defined is an error that occurs specifically in the Node.js environment. It happens when you try to use the Blob class, a standard browser API, in a version of Node.js where it is not available as a global object.

This guide will explain the root cause of this error and provide the two primary solutions: the modern solution for recent Node.js versions and the compatibility solution for older versions that require an explicit import.

The Core Problem: Browser APIs vs. Node.js Globals

The Blob class is a fundamental API in web browsers for representing raw, file-like data. It's a standard part of the web platform. However, Node.js is a server-side environment and has its own primary mechanism for handling binary data: the Buffer class.

For a long time, web-standard APIs like Blob, File, and fetch were not built into the Node.js global scope. If you tried to use them, you would get a ReferenceError.

Problem:

// Problem: This code runs perfectly in a browser, but...
// ...it will throw an error in older versions of Node.js.

const myBlob = new Blob(['Hello, World!']);

Error Output (in older Node.js versions):

ReferenceError: Blob is not defined

The Modern Solution (Node.js v15.7+): Blob is Global

To improve cross-platform compatibility between browser and server code, modern versions of Node.js have been adding support for more web-standard APIs to their global scope. The Blob class was made available globally starting in Node.js v15.7.0 (and backported to v14.18.0).

Solution: if you are using a recent version of Node.js, the Blob class is available automatically, and you don't need to do anything.

// This code works out-of-the-box in Node.js v15.7+

const myBlob = new Blob(['Hello, World!']);

console.log(myBlob); // Output: Blob { size: 13, type: '' }
note

If you are encountering this error, the first step is to check your Node.js version with the command node -v. If your version is older than v15.7, the best long-term solution is to upgrade your Node.js environment.

The Compatibility Solution (Older Node.js): Import from buffer

If you are required to work in an older Node.js environment where Blob is not a global, you can still access it. The Blob class is available, but it must be explicitly imported from Node.js's built-in buffer module.

The solution is to add an import statement at the top of your file.

For ES Modules (import syntax):

import { Blob } from 'buffer';

const myBlob = new Blob(['Hello, World!']);

console.log(myBlob); // Output: Blob { size: 13, type: '' }

For CommonJS (require syntax):

const { Blob } = require('buffer');

const myBlob = new Blob(['Hello, World!']);

console.log(myBlob); // Output: Blob { size: 13, type: '' }

This explicitly brings the Blob class into your file's local scope, resolving the ReferenceError.

Conclusion

The ReferenceError: Blob is not defined error is a clear indicator of an environment mismatch, specifically that you are trying to use a web API in a version of Node.js where it isn't globally available.

To solve it, follow this simple decision path:

  • Check your Node.js version (node -v).
  • If your version is v15.7.0 or newer, Blob should be globally available. If you're still getting the error, there may be a configuration issue with your test runner or bundler.
  • If your version is older than v15.7.0, you must explicitly import Blob from the buffer module:
    • ES Modules: import { Blob } from 'buffer';
    • CommonJS: const { Blob } = require('buffer');

The best long-term solution is to keep your Node.js environment up-to-date to take advantage of the web-standard APIs that are now included by default.