TypeError: crypto.randomUUID is not a function
Quick answer
crypto.randomUUID() was added to Node's built-in crypto module and later to the global Web Crypto object. This error usually means you're running an older Node runtime that predates it, or a different `crypto` (a browser polyfill, or a same-named local variable) is shadowing the built-in module.
What causes this error
randomUUID() is a relatively recent addition to Node's crypto module. If your runtime predates that Node version, or something else in scope has shadowed the name crypto (a local variable, a browser-oriented polyfill package), calling .randomUUID() on the wrong object throws this error.
The Fix
// Import explicitly from the node: prefix to avoid any shadowing ambiguity
import { randomUUID } from 'node:crypto';
const id = randomUUID();
// If you must support an older Node version, fall back to the uuid package
import { v4 as uuidv4 } from 'uuid';
const id = uuidv4();Common causes / variations
- Running on an old Node LTS line that predates `randomUUID`'s addition
- A local variable or import literally named `crypto` shadowing the built-in module
- Bundler configuration substituting a partial browser polyfill for `node:crypto`
Related errors
See also: Buffer() deprecated constructor, ERR_REQUIRE_ESM.
Was this fix helpful?
Comments
Loading comments...