JavaScript

How to Generate UUID in Node.js?

UUID stands for “Universally Unique Identifier” which contains 36 hexadecimal digits identifying the system resources. It is used in web applications, operating systems, databases, and many others. It is the 128-bit unique identifier that can be generated dynamically by following the basic syntax “xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx”. In this syntax, the “x” denotes the hexadecimal digits(0-9, A-F), “M” represents the version of UUID(1-5), and the “N” specifies the variant(1,5, A, or B).

This guide elaborates on how to generate UUID in Node.js with the following contents:

Prerequisites:
Before moving on to the practical implementation, look at the folder structure of the Node.js project:

Note: The JavaScript code to generate UUID will be written inside the “app.js” file of the Node.js project.

Let’s start with the “crypto” module.

Method 1: Generate UUID in Node.js Using “Crypto” Module

The “crypto” is the global module that offers the “randomUUID()” method for generating a UUID.

This method supports the random number generator which is also cryptographically secure to generate a random v4 Universally Unique Identifier.

The following code block shows its practical implementation:

import { randomUUID } from 'crypto'
console.log("UUID is " +crypto.randomUUID());

In the above code lines:

  • The “import” keyword imports the “randomUUID” method from the “crypto” module.
  • Next, the “console.log()” method utilizes the “randomUUID()” method to generate a UUID and display it in the console.

Output
Now, execute the following command to run the “.js” file:

node app.js

It can be seen that the terminal successfully shows the generated UUID:

Method 2: Generate UUID in Node.js Using the “UUID” Package

The user can also generate UUID with the help of the well-known package “uuid”. It creates a reliable UUID. Before using it in the Node.js project, the user first needs to add it with the help of the “npm” package manager:

npm install uuid

The terminal shows that the “uuid” package has been successfully added to the current NodeJS project.

Now, use the “uuid” package practically using the given code block:

const { v4: uuidv4 } = require('uuid');
const my_uuid = uuidv4();
console.log(my_uuid);

In the above code lines:

  • Firstly, the “require” keyword includes the installed module “uuid”.
  • Next, the “my_uuid” variable applies the “uuidv4()” method to generate the random UUID.
  • After that, the “console.log()” method displays the generated UUID.

Output
Now, run the “.js” file using the following command to see the output:

node app.js

It can be observed that the random UUID is generated that is displayed on the terminal:

Method 3: Generate UUIDs in Node.js Using the “Nano ID” Method

Another “npm” package that is used to generate a UUID is “Nano ID”. It is a more secure, reliable, and user-friendly string ID generator for JavaScript as compared to “uuid” and other smaller packages. Similar to “uuid” it can be added to the Node.js project easily by using the “npm” package manager:

npm install --save nanoid

In the above command, the “–save” is an optional flag that adds “nanoid” as a dependency in the “package.json” file of the Node.js project.

It can be seen that the above command is executed successfully by adding the “nanoid” in the current Node.js project:

Let’s use it practically to generate a UUID:

const Nanoid = require('nanoid');
const NanoidAsync = require('nanoid/async');
console.log(`UUID with Nano ID(sync): ${Nanoid.nanoid()}`);
(async function() {
 const nanoId = await NanoidAsync.nanoid();
 console.log(`UUID with Nano ID(async): ${nanoId}`);
})();

In the above code snippet:

  • The “require()” method includes the “nanoid” package in a synchronous and asynchronous way.
  • The “console.log()” method applies the “nanoid()” method to generate a UUID in a synchronous way i.e. stopping the execution of a program until the UUID is not generated.
  • The “async function” generates the UUID in an asynchronous way i.e. without blocking the program execution and then displays it in the console.

Output
Initiate the “.js” file using the stated command:

node app.js

The output is identical to the above two methods:

That’s all about generating a UUID in Node.js

Conclusion

In Node.js, to generate a UUID, use the “randomUUID()” method of the “crypto” module. This operation can also be achieved using the “uuid” or the “nanoid” packages. These packages require installation via the “npm” package manager. Once their installation is done, import them into the “.js” file of the Node.js project to generate a UUID. This guide has practically described all possible methods for generating a UUID in Node.js

About the author

Areej Nadeem

I am a technical author holding a Bachelor’s degree in Computer Science. I am passionate about writing and learning new technologies and sharing my knowledge with the rest of the world.