uuid
For the creation of RFC4122 UUIDs
Complete - Support for RFC4122 version 1, 3, 4, and 5 UUIDs
Cross-platform - Support for ...
CommonJS, ECMAScript Modules and CDN builds
Node 8, 10, 12, 14
Chrome, Safari, Firefox, Edge, IE 11 browsers
Webpack and rollup.js module bundlers
Secure - Cryptographically-strong random values
Small - Zero-dependency, small footprint, plays nice with "tree shaking" packagers
CLI - Includes the
uuidcommand line utility
Upgrading from [email protected]? Your code is probably okay, but check out Upgrading From [email protected] for details.
Quickstart
To create a random UUID...
1. Install
npm install uuid2. Create a UUID (ES6 module syntax)
... or using CommonJS syntax:
For timestamp UUIDs, namespace UUIDs, and other options read on ...
API Summary
API
uuid.NIL
The nil UUID string (all zeros).
Example:
uuid.parse(str)
Convert UUID string to array of bytes
str
A valid UUID String
returns
Uint8Array[16]
throws
TypeError if str is not a valid UUID
Note: Ordering of values in the byte arrays used by parse() and stringify() follows the left ↠ right order of hex-pairs in UUID strings. As shown in the example below.
Example:
uuid.stringify(arr[, offset])
Convert array of bytes to UUID string
arr
Array-like collection of 16 values (starting from offset) between 0-255.
[offset = 0]
Number Starting index in the Array
returns
String
throws
TypeError if a valid UUID string cannot be generated
Note: Ordering of values in the byte arrays used by parse() and stringify() follows the left ↠ right order of hex-pairs in UUID strings. As shown in the example below.
Example:
uuid.v1([options[, buffer[, offset]]])
Create an RFC version 1 (timestamp) UUID
[options]
Object with one or more of the following properties:
[options.node ]
RFC "node" field as an Array[6] of byte values (per 4.1.6)
[options.clockseq]
RFC "clock sequence" as a Number between 0 - 0x3fff
[options.msecs]
RFC "timestamp" field (Number of milliseconds, unix epoch)
[options.nsecs]
RFC "timestamp" field (Number of nanseconds to add to msecs, should be 0-10,000)
[options.random]
Array of 16 random bytes (0-255)
[options.rng]
Alternative to options.random, a Function that returns an Array of 16 random bytes (0-255)
[buffer]
Array | Buffer If specified, uuid will be written here in byte-form, starting at offset
[offset = 0]
Number Index to start writing UUID bytes in buffer
returns
UUID String if no buffer is specified, otherwise returns buffer
throws
Error if more than 10M UUIDs/sec are requested
Note: The default node id (the last 12 digits in the UUID) is generated once, randomly, on process startup, and then remains unchanged for the duration of the process.
Note: options.random and options.rng are only meaningful on the very first call to v1(), where they may be passed to initialize the internal node and clockseq fields.
Example:
Example using options:
uuid.v3(name, namespace[, buffer[, offset]])
Create an RFC version 3 (namespace w/ MD5) UUID
API is identical to v5(), but uses "v3" instead.
⚠️ Note: Per the RFC, "If backward compatibility is not an issue, SHA-1 [Version 5] is preferred."
uuid.v4([options[, buffer[, offset]]])
Create an RFC version 4 (random) UUID
[options]
Object with one or more of the following properties:
[options.random]
Array of 16 random bytes (0-255)
[options.rng]
Alternative to options.random, a Function that returns an Array of 16 random bytes (0-255)
[buffer]
Array | Buffer If specified, uuid will be written here in byte-form, starting at offset
[offset = 0]
Number Index to start writing UUID bytes in buffer
returns
UUID String if no buffer is specified, otherwise returns buffer
Example:
Example using predefined random values:
uuid.v5(name, namespace[, buffer[, offset]])
Create an RFC version 5 (namespace w/ SHA-1) UUID
name
String | Array
namespace
String | Array[16] Namespace UUID
[buffer]
Array | Buffer If specified, uuid will be written here in byte-form, starting at offset
[offset = 0]
Number Index to start writing UUID bytes in buffer
returns
UUID String if no buffer is specified, otherwise returns buffer
Note: The RFC DNS and URL namespaces are available as v5.DNS and v5.URL.
Example with custom namespace:
Example with RFC URL namespace:
uuid.validate(str)
Test a string to see if it is a valid UUID
str
String to validate
returns
true if string is a valid UUID, false otherwise
Example:
Using validate and version together it is possible to do per-version validation, e.g. validate for only v4 UUIds.
uuid.version(str)
Detect RFC version of a UUID
str
A valid UUID String
returns
Number The RFC version of the UUID
throws
TypeError if str is not a valid UUID
Example:
Command Line
UUIDs can be generated from the command line using uuid.
The default is to generate version 4 UUIDS, however the other versions are supported. Type uuid --help for details:
ECMAScript Modules
This library comes with ECMAScript Modules (ESM) support for Node.js versions that support it (example) as well as bundlers like rollup.js (example) and webpack (example) (targeting both, Node.js and browser environments).
To run the examples you must first create a dist build of this library in the module root:
CDN Builds
ECMAScript Modules
To load this module directly into modern browsers that support loading ECMAScript Modules you can make use of jspm:
UMD
To load this module directly into older browsers you can use the UMD (Universal Module Definition) builds from any of the following CDNs:
Using UNPKG:
Using jsDelivr:
Using cdnjs:
These CDNs all provide the same uuidv4() method:
Methods for the other algorithms (uuidv1(), uuidv3() and uuidv5()) are available from the files uuidv1.min.js, uuidv3.min.js and uuidv5.min.js respectively.
"getRandomValues() not supported"
This error occurs in environments where the standard crypto.getRandomValues() API is not supported. This issue can be resolved by adding an appropriate polyfill:
React Native / Expo
Install
react-native-get-random-valuesImport it before
uuid. Sinceuuidmight also appear as a transitive dependency of some other imports it's safest to just importreact-native-get-random-valuesas the very first thing in your entry point:
Note: If you are using Expo, you must be using at least [email protected] and [email protected].
Web Workers / Service Workers (Edge <= 18)
In Edge <= 18, Web Crypto is not supported in Web Workers or Service Workers and we are not aware of a polyfill (let us know if you find one, please).
Upgrading From [email protected]
[email protected]Only Named Exports Supported When Using with Node.js ESM
[email protected] did not come with native ECMAScript Module (ESM) support for Node.js. Importing it in Node.js ESM consequently imported the CommonJS source with a default export. This library now comes with true Node.js ESM support and only provides named exports.
Instead of doing:
you will now have to use the named exports:
Deep Requires No Longer Supported
Deep requires like require('uuid/v4') which have been deprecated in [email protected] are no longer supported.
Upgrading From [email protected]
[email protected]"Wait... what happened to [email protected] - [email protected]?!?"
In order to avoid confusion with RFC version 4 and version 5 UUIDs, and a possible version 6, releases 4 thru 6 of this module have been skipped.
Deep Requires Now Deprecated
[email protected] encouraged the use of deep requires to minimize the bundle size of browser builds:
As of [email protected] this library now provides ECMAScript modules builds, which allow packagers like Webpack and Rollup to do "tree-shaking" to remove dead code. Instead, use the import syntax:
... or for CommonJS:
Default Export Removed
[email protected] was exporting the Version 4 UUID method as a default export:
This usage pattern was already discouraged in [email protected] and has been removed in [email protected].
Markdown generated from README_js.md by 
Last updated