Creating a Blob in Node.js fails or the Blob global is missing
Blob is not definedShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
The first command prints a version below v18 and 'undefined'The runtime is older than Node.js 18Fix - 2
The 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
You 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
- 1
What does the error look like?
Blob is not definedNode.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 node2Fix
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
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(" src2Fix
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
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()" src2Fix
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.binConfirmed by nodejs.org
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix