How to Generate a UUID in Node.js

    Use built-in crypto for v4, and choose a v7 API supported by your deployed Node version.

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

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

    Node’s built-in randomUUID generates v4 without dependencies. It was introduced in 14.17.0 and 15.6.0; these are historical minimums, not deployment recommendations.

    For v7 on Node 24, use a maintained library such as uuid. Node 26.1+ also provides randomUUIDv7. Keep runtime-specific code separate from browser code.

    Built-in UUID v4: ESM

    Save this as an .mjs file, or use a project configured with type: module. Each call returns a new random v4 UUID.

    javascript
    import { randomUUID } from "node:crypto";
    const id = randomUUID();
    console.log(id);

    Built-in UUID v4: CommonJS

    The built-in module also supports CommonJS. Save this example as .cjs.

    javascript
    const { randomUUID } = require("node:crypto");
    console.log(randomUUID());

    UUID v7 on Node 24 with uuid

    Run npm install uuid, then use ESM imports. Starting with uuid 12 the package no longer provides CommonJS support; do not assume require("uuid") works across supported runtimes. This example uses the same v7 API as FastUUID.

    javascript
    import { v7, validate, version } from "uuid";
    const id = v7();
    console.assert(validate(id) && version(id) === 7);
    console.log(id);

    Built-in UUID v7 on Node 26.1+

    This separate function was added in Node 26.1.0 and is not available on Node 24. Check your production runtime before using it. Its clock is not monotonic, so do not assume strict generation order.

    javascript
    import { randomUUIDv7 } from "node:crypto";
    console.log(randomUUIDv7());

    Repeatable UUID v5

    A namespace and name produce a deterministic ID. This fixture uses the standard DNS namespace. It is public test data, not a secret.

    javascript
    import { v5 } from "uuid";
    const id = v5("www.example.com", v5.DNS);
    console.assert(id === "2ed6657d-e927-568b-95e1-2665a8aea6a2");

    Frequently asked questions

    Do I need npm uuid for Node UUID v4?
    No. Import randomUUID from node:crypto.
    How do I generate UUID v7 in Node 24?
    Install uuid and use its v7 ESM export. Node 26.1+ also offers a separate built-in randomUUIDv7 method.
    Why does require("uuid") fail?
    uuid 12 and later no longer provide CommonJS support. Use ESM imports and check the installed package’s supported Node versions.

    Official references