NPM packages to generate Unique IDs for your next project 😎
Tags: javascript, npm, webdev, codenewbie
Published: 2022-05-23 05:40:20

While tweeting something or sharing something over Linkedin requires a unique id to identify a particular post. On the daily basis, we need to store or retrieve data uniquely & generating random numbers is not a cool idea. So through the post, we will take a look at the most commonly used NPM packages which are used for generating unique Ids.


![Game On](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7j03ief7o85kox3rsmal.gif)


**1️⃣ [UUID](https://www.npmjs.com/package/uuid) -** UUID is the **most popular** npm package used for generating unique Ids. The npm package supports node version 10, 12, 14, and 16 & also provides support for React Native & browsers. It also supports version 1, 3, 4, and 5.
The Package can be installed by the **`npm install uuid`** command. It provides support for both **ES6 and CommonJS** syntax.
#### Using the ES6 module syntax -

```
import { v4 as uuidv4 } from 'uuid';

const userId=uuidv4();
console.log(userId); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
```
#### Using the CommonJS syntax -

```
const { v4: uuidv4 } = require('uuid');

const userId=uuidv4();
console.log(userId); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
```

- 
The Module also provides a bunch of methods & methods for versions, check out all of them here 👉 [Github](https://github.com/uuidjs/uuid#uuidv4options-buffer-offset)


**2️⃣ [Nano ID](https://github.com/ai/nanoid) -** Nano ID is **very popular**, tiny, **URL-friendly** & 2 times faster than uuid.

The package can be installed by **`npm install --save nanoid`** command. Like uuid, it also provides support for both **ES6 and CommonJS** syntax.

#### Let's see an example through ES6 syntax -

```
import { nanoid } from 'nanoid'

const userId=nanoid();
console.log(userId); // => "V1StGXR8_Z5jdHi6B-myT"
```

- Suppose, if we need to reduce the id size to 10, then we can pass size as an argument like this -

```
nanoid(10) //=> "IRFa-VaY2b"
```

**3️⃣ [Cuid](https://www.npmjs.com/package/cuid) -** 

- Install using the command **`npm i cuid`**

- Provides support for both ES6 and CommonJS syntax.

- Example(using ES module syntax) -

```
import cuid from 'cuid';

console.log( cuid() );

// cjld2cjxh0000qzrmn831i7rn
```

**4️⃣ [crypto-random-string](https://github.com/sindresorhus/crypto-random-string) -** It generates cryptographically strong random string. 

- Works in Node.js and browsers.
- Install using the command **`npm install crypto-random-string`**
- Example -

```
import cryptoRandomString from 'crypto-random-string';

cryptoRandomString({length: 10});
//=> '2cf05d94db'
```

- It also supports a second argument **type**(optional) along with length. The Default value for the type is **hex**.

**5️⃣ [uniqid](https://github.com/adamhalasz/uniqid) -**

- Install using the command **`npm install uniqid`**.
- Unique Hexatridecimal ID generator.
- Creates unique IDs based on the current time, process, and machine name.
- Works in Node.js and browsers.

```
import uniqid from 'uniqid';

console.log(uniqid()); // -> 4n5pxq24kpiob12og9
```



🏁 We have discussed the npm packages which are used for generating unique IDs. If you use a different package feel free to mention it in the comments.