crypto.randomUUID(): Syntax and Troubleshooting

    Generate UUID v4 and diagnose secure-context or runtime-support errors.

    Need a UUID right now? Generate one instantly in your browser.

    Reviewed 2026-09-11. Check runtime requirements before running an example.

    Browser crypto.randomUUID() and Node’s node:crypto randomUUID() both produce random version 4 UUID strings. Their availability and options differ.

    In browsers, randomUUID requires a secure context. Node’s module function was added in versions 14.17.0 and 15.6.0; use a currently supported Node release for deployment.

    Browser syntax

    The browser method takes no arguments. Check both the context and the method: the existence of crypto alone does not establish randomUUID support.

    javascript
    console.log("Secure context:", globalThis.isSecureContext);
    console.log("UUID API:", typeof globalThis.crypto?.randomUUID);
    if (typeof globalThis.crypto?.randomUUID === "function") {
      console.log(globalThis.crypto.randomUUID());
    }

    Why randomUUID is not a function

    Check whether the page is on HTTPS or a trusted localhost origin. Test for the method, update an unsupported browser, and inspect whether an application variable is shadowing globalThis.crypto. An HTTP address on a local network is not necessarily a secure context.

    Do not treat this error as evidence that all Web Crypto is missing. getRandomValues can be available when randomUUID is not. See the browser guide for a fallback that preserves cryptographic randomness.

    Node.js syntax and entropy caching

    Import the module function explicitly. Node accepts disableEntropyCache; the browser method does not expose that option. Disabling Node’s cache is not needed for ordinary identifier generation.

    javascript
    import { randomUUID } from "node:crypto";
    console.log(randomUUID());
    console.log(randomUUID({ disableEntropyCache: true }));

    Version and security boundaries

    randomUUID returns v4, not v7. Newer Node releases have a separate randomUUIDv7 method; consult the Node.js guide for its version requirement.

    Random identifiers do not replace authorization. Use dedicated secret-token APIs for credentials. UUID generation does not guarantee that a value is absent from your database; retain uniqueness constraints.

    Frequently asked questions

    Why is crypto.randomUUID undefined?
    Possible causes include an insecure browser context, an older runtime or a variable shadowing the global crypto object. Check isSecureContext and typeof globalThis.crypto?.randomUUID.
    Which Node version added randomUUID?
    The node:crypto module method was added in Node 14.17.0 and 15.6.0. Browser and Node global Web Crypto availability are separate questions.
    Can randomUUID produce a UUID v7?
    No. randomUUID produces version 4. Use a v7 implementation or Node’s separate randomUUIDv7 method where supported.

    Official references