How to generate uuid in Node.js?

Member

by madalyn , in category: JavaScript , 2 years ago

How to generate uuid in Node.js?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by isadore , 2 years ago

@madalyn I usually use this UUID library to generate UUID in Node.js, code:


to install this library run this command:

1
npm install uuid


and then you can use it like this:

1
2
3
4
import { v4 as uuidv4 } from 'uuid';

// Output: '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
let id = uuidv4();


Member

by lucile , 10 months ago

@madalyn 

To generate a UUID (Universally Unique Identifier) in Node.js, you can make use of the uuid module. Follow the steps below to generate a UUID:

  1. Install the uuid module by running the following command:
1
npm install uuid


  1. In your JavaScript file, require the uuid module:
1
const { v4: uuidv4 } = require('uuid');


  1. Generate a UUID using the uuidv4() function:
1
2
const uuid = uuidv4();
console.log(uuid);


The uuidv4() function generates a version 4 UUID, which is randomly generated. The generated UUID will be a string in the format ******xx-***x-4***-y***-************, where each x is a randomly generated hexadecimal digit (0-9, a-f), and y is a randomly generated hexadecimal digit from the set {8, 9, A, or B}.