Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Creating a Blob in Node.js fails or the Blob global is missing

ErrorBlob is not defined

Short answer

A Blob global exists in Node.js from v18.0.0. On older versions import it with require('node:buffer'). The constructor takes an array of parts, so wrap a Buffer or string in [ ]. To get bytes back, await blob.arrayBuffer() or blob.text(); both are asynchronous.

  • Applies to Node.js 18, Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the Node.js version the script really runs under. The global Blob only exists from v18.0.0.

    node -p "process.version + ' ' + typeof Blob"
  2. 2Match what you see, then jump to its fix

    1. 1The first command prints a version below v18 and 'undefined'The runtime is older than Node.js 18Fix
    2. 2The Blob's size or content is not what you passed in, or the constructor rejects the argumentThe first argument is not an array of partsFix
    3. 3You get a Promise object where you expected bytes or a string, so writing it to disk or a response failsReading the Blob back as if it were synchronousFix

What does the error look like?

  • Blob is not defined
  • Node.js can't create Blobs

Cause 1 of 3

The runtime is older than Node.js 18

1Check

The Node.js globals page lists Blob as added in v18.0.0. Check which node a service, container or CI job really uses, since it can differ from your shell.

node -v
which node

2Fix

Upgrade to a supported Node.js line. If you must stay on an old one, import the class from node:buffer, where it has existed since v15.7.0 and v14.18.0.

const { Blob } = require('node:buffer');
const blob = new Blob(['hello'], { type: 'text/plain' });

3Verify

The class constructs and reports its size.

node -e "const {Blob}=require('node:buffer'); console.log(new Blob(['hi']).size)"

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 3

The first argument is not an array of parts

1Check

The constructor documents its first argument as an array whose items are strings, ArrayBuffers, TypedArrays, DataViews or Blobs. A Buffer is a Uint8Array, so it fits as one item, but it must sit inside the array.

grep -rn "new Blob(" src

2Fix

Wrap the data in brackets, and set the MIME type through options.

const buf = Buffer.from('hello world', 'utf8');
const blob = new Blob([buf], { type: 'audio/mpeg' });

3Verify

Read it back as text.

node -e "new Blob([Buffer.from('ok')]).text().then(console.log)"

Confirmed by nodejs.org

Did this fix it?

Cause 3 of 3

Reading the Blob back as if it were synchronous

1Check

blob.arrayBuffer() resolves to an ArrayBuffer copy and blob.text() resolves to a UTF-8 string; neither returns a value directly.

grep -rn "arrayBuffer()\|\.text()" src

2Fix

Await the call, and wrap the ArrayBuffer in a Buffer when an API such as fs wants one.

import { writeFile } from 'node:fs/promises';
const bytes = Buffer.from(await blob.arrayBuffer());
await writeFile('out.bin', bytes);

3Verify

The file exists and has the same size as the Blob.

ls -l out.bin

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Pin the Node.js version in package.json engines and in your Dockerfile so every environment agrees.
  • Import Blob from node:buffer in shared code that must also run on older runtimes.
  • Treat Blob data as copies: the sources you pass in are copied, so later changes to the Buffer do not change the Blob.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.