Create a valid UUID in a project
๐ฏcontext
You have a TypeScript or JavaScript project and want to create a valid UUID in order to use it as an unique identifier for an object.
Descriptionโ
This How-To will show you how you can create valid UUIDs easily with the help of the uuid NPM module.
Stepsโ
-
Install uuid npm module
npm install uuid -
If you use TypeScript, please also install the types as a dev dependency
npm install --save-dev @types/uuid -
Use the uuid module to create a new uuid
import {โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ v4 as uuidv4 }โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ from 'uuid';
// create a new v4 uuid
const myUuid = uuidv4(); // -> '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'