<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>README</title>
<style>
html {
line-height: 1.5;
font-family: Georgia, serif;
font-size: 20px;
color: #1a1a1a;
background-color: #fdfdfd;
}
body {
margin: 0 auto;
max-width: 36em;
padding-left: 50px;
padding-right: 50px;
padding-top: 50px;
padding-bottom: 50px;
hyphens: auto;
overflow-wrap: break-word;
text-rendering: optimizeLegibility;
font-kerning: normal;
}
@media (max-width: 600px) {
body {
font-size: 0.9em;
padding: 1em;
}
h1 {
font-size: 1.8em;
}
}
@media print {
body {
background-color: transparent;
color: black;
font-size: 12pt;
}
p, h2, h3 {
orphans: 3;
widows: 3;
}
h2, h3, h4 {
page-break-after: avoid;
}
}
p {
margin: 1em 0;
}
a {
color: #1a1a1a;
}
a:visited {
color: #1a1a1a;
}
img {
max-width: 100%;
}
h1, h2, h3, h4, h5, h6 {
margin-top: 1.4em;
}
h5, h6 {
font-size: 1em;
font-style: italic;
}
h6 {
font-weight: normal;
}
ol, ul {
padding-left: 1.7em;
margin-top: 1em;
}
li > ol, li > ul {
margin-top: 0;
}
blockquote {
margin: 1em 0 1em 1.7em;
padding-left: 1em;
border-left: 2px solid #e6e6e6;
color: #606060;
}
code {
font-family: Menlo, Monaco, 'Lucida Console', Consolas, monospace;
font-size: 85%;
margin: 0;
}
pre {
margin: 1em 0;
overflow: auto;
}
pre code {
padding: 0;
overflow: visible;
overflow-wrap: normal;
}
.sourceCode {
background-color: transparent;
overflow: visible;
}
hr {
background-color: #1a1a1a;
border: none;
height: 1px;
margin: 1em 0;
}
table {
margin: 1em 0;
border-collapse: collapse;
width: 100%;
overflow-x: auto;
display: block;
font-variant-numeric: lining-nums tabular-nums;
}
table caption {
margin-bottom: 0.75em;
}
tbody {
margin-top: 0.5em;
border-top: 1px solid #1a1a1a;
border-bottom: 1px solid #1a1a1a;
}
th {
border-top: 1px solid #1a1a1a;
padding: 0.25em 0.5em 0.25em 0.5em;
}
td {
padding: 0.125em 0.5em 0.25em 0.5em;
}
header {
margin-bottom: 4em;
text-align: center;
}
#TOC li {
list-style: none;
}
#TOC ul {
padding-left: 1.3em;
}
#TOC > ul {
padding-left: 0;
}
#TOC a:not(:hover) {
text-decoration: none;
}
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
</style>
</head>
<body>
<!--
-- This file is auto-generated from README_js.md. Changes should be made there.
-->
<p>For the creation of <a
href="http://www.ietf.org/rfc/rfc4122.txt">RFC4122</a> UUIDs</p>
<ul>
<li><strong>Complete</strong> - Support for RFC4122 version 1, 3, 4, and
5 UUIDs</li>
<li><strong>Cross-platform</strong> - Support for ...
<ul>
<li>CommonJS, <a href="#ecmascript-modules">ECMAScript Modules</a> and
<a href="#cdn-builds">CDN builds</a></li>
<li>Node 8, 10, 12, 14</li>
<li>Chrome, Safari, Firefox, Edge, IE 11 browsers</li>
<li>Webpack and rollup.js module bundlers</li>
<li><a href="#react-native--expo">React Native / Expo</a></li>
</ul></li>
<li><strong>Secure</strong> - Cryptographically-strong random
values</li>
<li><strong>Small</strong> - Zero-dependency, small footprint, plays
nice with "tree shaking" packagers</li>
<li><strong>CLI</strong> - Includes the <a
href="#command-line"><code>uuid</code> command line</a> utility</li>
</ul>
<p><strong>Upgrading from <code>uuid@3.x</code>?</strong> Your code is
probably okay, but check out <a href="#upgrading-from-uuid3x">Upgrading
From <code>uuid@3.x</code></a> for details.</p>
<h2 id="quickstart">Quickstart</h2>
<p>To create a random UUID...</p>
<p><strong>1. Install</strong></p>
<pre class="shell"><code>npm install uuid</code></pre>
<p><strong>2. Create a UUID</strong> (ES6 module syntax)</p>
<div class="sourceCode" id="cb2"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { v4 <span class="im">as</span> uuidv4 } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidv4</span>()<span class="op">;</span> <span class="co">// ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'</span></span></code></pre></div>
<p>... or using CommonJS syntax:</p>
<div class="sourceCode" id="cb3"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> { <span class="dt">v4</span><span class="op">:</span> uuidv4 } <span class="op">=</span> <span class="pp">require</span>(<span class="st">'uuid'</span>)<span class="op">;</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidv4</span>()<span class="op">;</span> <span class="co">// ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'</span></span></code></pre></div>
<p>For timestamp UUIDs, namespace UUIDs, and other options read on
...</p>
<h2 id="api-summary">API Summary</h2>
<table>
<tbody>
<tr class="odd">
<td><a href="#uuidnil"><code>uuid.NIL</code></a></td>
<td>The nil UUID string (all zeros)</td>
<td>New in <code>uuid@8.3</code></td>
</tr>
<tr class="even">
<td><a href="#uuidparsestr"><code>uuid.parse()</code></a></td>
<td>Convert UUID string to array of bytes</td>
<td>New in <code>uuid@8.3</code></td>
</tr>
<tr class="odd">
<td><a
href="#uuidstringifyarr-offset"><code>uuid.stringify()</code></a></td>
<td>Convert array of bytes to UUID string</td>
<td>New in <code>uuid@8.3</code></td>
</tr>
<tr class="even">
<td><a
href="#uuidv1options-buffer-offset"><code>uuid.v1()</code></a></td>
<td>Create a version 1 (timestamp) UUID</td>
<td></td>
</tr>
<tr class="odd">
<td><a
href="#uuidv3name-namespace-buffer-offset"><code>uuid.v3()</code></a></td>
<td>Create a version 3 (namespace w/ MD5) UUID</td>
<td></td>
</tr>
<tr class="even">
<td><a
href="#uuidv4options-buffer-offset"><code>uuid.v4()</code></a></td>
<td>Create a version 4 (random) UUID</td>
<td></td>
</tr>
<tr class="odd">
<td><a
href="#uuidv5name-namespace-buffer-offset"><code>uuid.v5()</code></a></td>
<td>Create a version 5 (namespace w/ SHA-1) UUID</td>
<td></td>
</tr>
<tr class="even">
<td><a href="#uuidvalidatestr"><code>uuid.validate()</code></a></td>
<td>Test a string to see if it is a valid UUID</td>
<td>New in <code>uuid@8.3</code></td>
</tr>
<tr class="odd">
<td><a href="#uuidversionstr"><code>uuid.version()</code></a></td>
<td>Detect RFC version of a UUID</td>
<td>New in <code>uuid@8.3</code></td>
</tr>
</tbody>
</table>
<h2 id="api">API</h2>
<h3 id="uuidnil">uuid.NIL</h3>
<p>The nil UUID string (all zeros).</p>
<p>Example:</p>
<div class="sourceCode" id="cb4"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { NIL <span class="im">as</span> NIL_UUID } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>NIL_UUID<span class="op">;</span> <span class="co">// ⇨ '00000000-0000-0000-0000-000000000000'</span></span></code></pre></div>
<h3 id="uuidparsestr">uuid.parse(str)</h3>
<p>Convert UUID string to array of bytes</p>
<table>
<tbody>
<tr class="odd">
<td><code>str</code></td>
<td>A valid UUID <code>String</code></td>
</tr>
<tr class="even">
<td><em>returns</em></td>
<td><code>Uint8Array[16]</code></td>
</tr>
<tr class="odd">
<td><em>throws</em></td>
<td><code>TypeError</code> if <code>str</code> is not a valid UUID</td>
</tr>
</tbody>
</table>
<p>Note: Ordering of values in the byte arrays used by
<code>parse()</code> and <code>stringify()</code> follows the left ↠
right order of hex-pairs in UUID strings. As shown in the example
below.</p>
<p>Example:</p>
<div class="sourceCode" id="cb5"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { parse <span class="im">as</span> uuidParse } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="co">// Parse a UUID</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> bytes <span class="op">=</span> <span class="fu">uuidParse</span>(<span class="st">'6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'</span>)<span class="op">;</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a><span class="co">// Convert to hex strings to show byte order (for documentation purposes)</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a>[<span class="op">...</span>bytes]<span class="op">.</span><span class="fu">map</span>((v) <span class="kw">=></span> v<span class="op">.</span><span class="fu">toString</span>(<span class="dv">16</span>)<span class="op">.</span><span class="fu">padStart</span>(<span class="dv">2</span><span class="op">,</span> <span class="st">'0'</span>))<span class="op">;</span> <span class="co">// ⇨ </span></span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="co">// [</span></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="co">// '6e', 'c0', 'bd', '7f',</span></span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="co">// '11', 'c0', '43', 'da',</span></span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> <span class="co">// '97', '5e', '2a', '8a',</span></span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a> <span class="co">// 'd9', 'eb', 'ae', '0b'</span></span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a> <span class="co">// ]</span></span></code></pre></div>
<h3 id="uuidstringifyarr-offset">uuid.stringify(arr[, offset])</h3>
<p>Convert array of bytes to UUID string</p>
<table>
<tbody>
<tr class="odd">
<td><code>arr</code></td>
<td><code>Array</code>-like collection of 16 values (starting from
<code>offset</code>) between 0-255.</td>
</tr>
<tr class="even">
<td>[<code>offset</code> = 0]</td>
<td><code>Number</code> Starting index in the Array</td>
</tr>
<tr class="odd">
<td><em>returns</em></td>
<td><code>String</code></td>
</tr>
<tr class="even">
<td><em>throws</em></td>
<td><code>TypeError</code> if a valid UUID string cannot be
generated</td>
</tr>
</tbody>
</table>
<p>Note: Ordering of values in the byte arrays used by
<code>parse()</code> and <code>stringify()</code> follows the left ↠
right order of hex-pairs in UUID strings. As shown in the example
below.</p>
<p>Example:</p>
<div class="sourceCode" id="cb6"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { stringify <span class="im">as</span> uuidStringify } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> uuidBytes <span class="op">=</span> [</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x6e</span><span class="op">,</span></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xc0</span><span class="op">,</span></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xbd</span><span class="op">,</span></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x7f</span><span class="op">,</span></span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x11</span><span class="op">,</span></span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xc0</span><span class="op">,</span></span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x43</span><span class="op">,</span></span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xda</span><span class="op">,</span></span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x97</span><span class="op">,</span></span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x5e</span><span class="op">,</span></span>
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x2a</span><span class="op">,</span></span>
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x8a</span><span class="op">,</span></span>
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xd9</span><span class="op">,</span></span>
<span id="cb6-17"><a href="#cb6-17" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xeb</span><span class="op">,</span></span>
<span id="cb6-18"><a href="#cb6-18" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xae</span><span class="op">,</span></span>
<span id="cb6-19"><a href="#cb6-19" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x0b</span><span class="op">,</span></span>
<span id="cb6-20"><a href="#cb6-20" aria-hidden="true" tabindex="-1"></a>]<span class="op">;</span></span>
<span id="cb6-21"><a href="#cb6-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-22"><a href="#cb6-22" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidStringify</span>(uuidBytes)<span class="op">;</span> <span class="co">// ⇨ '6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'</span></span></code></pre></div>
<h3 id="uuidv1options-buffer-offset">uuid.v1([options[, buffer[,
offset]]])</h3>
<p>Create an RFC version 1 (timestamp) UUID</p>
<table>
<tbody>
<tr class="odd">
<td>[<code>options</code>]</td>
<td><code>Object</code> with one or more of the following
properties:</td>
</tr>
<tr class="even">
<td>[<code>options.node</code> ]</td>
<td>RFC "node" field as an <code>Array[6]</code> of byte values (per
4.1.6)</td>
</tr>
<tr class="odd">
<td>[<code>options.clockseq</code>]</td>
<td>RFC "clock sequence" as a <code>Number</code> between 0 -
0x3fff</td>
</tr>
<tr class="even">
<td>[<code>options.msecs</code>]</td>
<td>RFC "timestamp" field (<code>Number</code> of milliseconds, unix
epoch)</td>
</tr>
<tr class="odd">
<td>[<code>options.nsecs</code>]</td>
<td>RFC "timestamp" field (<code>Number</code> of nanseconds to add to
<code>msecs</code>, should be 0-10,000)</td>
</tr>
<tr class="even">
<td>[<code>options.random</code>]</td>
<td><code>Array</code> of 16 random bytes (0-255)</td>
</tr>
<tr class="odd">
<td>[<code>options.rng</code>]</td>
<td>Alternative to <code>options.random</code>, a <code>Function</code>
that returns an <code>Array</code> of 16 random bytes (0-255)</td>
</tr>
<tr class="even">
<td>[<code>buffer</code>]</td>
<td><code>Array | Buffer</code> If specified, uuid will be written here
in byte-form, starting at <code>offset</code></td>
</tr>
<tr class="odd">
<td>[<code>offset</code> = 0]</td>
<td><code>Number</code> Index to start writing UUID bytes in
<code>buffer</code></td>
</tr>
<tr class="even">
<td><em>returns</em></td>
<td>UUID <code>String</code> if no <code>buffer</code> is specified,
otherwise returns <code>buffer</code></td>
</tr>
<tr class="odd">
<td><em>throws</em></td>
<td><code>Error</code> if more than 10M UUIDs/sec are requested</td>
</tr>
</tbody>
</table>
<p>Note: The default <a
href="https://tools.ietf.org/html/rfc4122#section-4.1.6">node id</a>
(the last 12 digits in the UUID) is generated once, randomly, on process
startup, and then remains unchanged for the duration of the process.</p>
<p>Note: <code>options.random</code> and <code>options.rng</code> are
only meaningful on the very first call to <code>v1()</code>, where they
may be passed to initialize the internal <code>node</code> and
<code>clockseq</code> fields.</p>
<p>Example:</p>
<div class="sourceCode" id="cb7"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { v1 <span class="im">as</span> uuidv1 } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidv1</span>()<span class="op">;</span> <span class="co">// ⇨ '2c5ea4c0-4067-11e9-8bad-9b1deb4d3b7d'</span></span></code></pre></div>
<p>Example using <code>options</code>:</p>
<div class="sourceCode" id="cb8"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { v1 <span class="im">as</span> uuidv1 } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> v1options <span class="op">=</span> {</span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="dt">node</span><span class="op">:</span> [<span class="bn">0x01</span><span class="op">,</span> <span class="bn">0x23</span><span class="op">,</span> <span class="bn">0x45</span><span class="op">,</span> <span class="bn">0x67</span><span class="op">,</span> <span class="bn">0x89</span><span class="op">,</span> <span class="bn">0xab</span>]<span class="op">,</span></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a> <span class="dt">clockseq</span><span class="op">:</span> <span class="bn">0x1234</span><span class="op">,</span></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a> <span class="dt">msecs</span><span class="op">:</span> <span class="kw">new</span> <span class="bu">Date</span>(<span class="st">'2011-11-01'</span>)<span class="op">.</span><span class="fu">getTime</span>()<span class="op">,</span></span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a> <span class="dt">nsecs</span><span class="op">:</span> <span class="dv">5678</span><span class="op">,</span></span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a>}<span class="op">;</span></span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidv1</span>(v1options)<span class="op">;</span> <span class="co">// ⇨ '710b962e-041c-11e1-9234-0123456789ab'</span></span></code></pre></div>
<h3 id="uuidv3name-namespace-buffer-offset">uuid.v3(name, namespace[,
buffer[, offset]])</h3>
<p>Create an RFC version 3 (namespace w/ MD5) UUID</p>
<p>API is identical to <code>v5()</code>, but uses "v3" instead.</p>
<p>⚠️ Note: Per the RFC, "<em>If backward compatibility is not an issue,
SHA-1 [Version 5] is preferred</em>."</p>
<h3 id="uuidv4options-buffer-offset">uuid.v4([options[, buffer[,
offset]]])</h3>
<p>Create an RFC version 4 (random) UUID</p>
<table>
<tbody>
<tr class="odd">
<td>[<code>options</code>]</td>
<td><code>Object</code> with one or more of the following
properties:</td>
</tr>
<tr class="even">
<td>[<code>options.random</code>]</td>
<td><code>Array</code> of 16 random bytes (0-255)</td>
</tr>
<tr class="odd">
<td>[<code>options.rng</code>]</td>
<td>Alternative to <code>options.random</code>, a <code>Function</code>
that returns an <code>Array</code> of 16 random bytes (0-255)</td>
</tr>
<tr class="even">
<td>[<code>buffer</code>]</td>
<td><code>Array | Buffer</code> If specified, uuid will be written here
in byte-form, starting at <code>offset</code></td>
</tr>
<tr class="odd">
<td>[<code>offset</code> = 0]</td>
<td><code>Number</code> Index to start writing UUID bytes in
<code>buffer</code></td>
</tr>
<tr class="even">
<td><em>returns</em></td>
<td>UUID <code>String</code> if no <code>buffer</code> is specified,
otherwise returns <code>buffer</code></td>
</tr>
</tbody>
</table>
<p>Example:</p>
<div class="sourceCode" id="cb9"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { v4 <span class="im">as</span> uuidv4 } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidv4</span>()<span class="op">;</span> <span class="co">// ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'</span></span></code></pre></div>
<p>Example using predefined <code>random</code> values:</p>
<div class="sourceCode" id="cb10"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { v4 <span class="im">as</span> uuidv4 } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> v4options <span class="op">=</span> {</span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a> <span class="dt">random</span><span class="op">:</span> [</span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x10</span><span class="op">,</span></span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x91</span><span class="op">,</span></span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x56</span><span class="op">,</span></span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xbe</span><span class="op">,</span></span>
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xc4</span><span class="op">,</span></span>
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xfb</span><span class="op">,</span></span>
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xc1</span><span class="op">,</span></span>
<span id="cb10-12"><a href="#cb10-12" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xea</span><span class="op">,</span></span>
<span id="cb10-13"><a href="#cb10-13" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x71</span><span class="op">,</span></span>
<span id="cb10-14"><a href="#cb10-14" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xb4</span><span class="op">,</span></span>
<span id="cb10-15"><a href="#cb10-15" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xef</span><span class="op">,</span></span>
<span id="cb10-16"><a href="#cb10-16" aria-hidden="true" tabindex="-1"></a> <span class="bn">0xe1</span><span class="op">,</span></span>
<span id="cb10-17"><a href="#cb10-17" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x67</span><span class="op">,</span></span>
<span id="cb10-18"><a href="#cb10-18" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x1c</span><span class="op">,</span></span>
<span id="cb10-19"><a href="#cb10-19" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x58</span><span class="op">,</span></span>
<span id="cb10-20"><a href="#cb10-20" aria-hidden="true" tabindex="-1"></a> <span class="bn">0x36</span><span class="op">,</span></span>
<span id="cb10-21"><a href="#cb10-21" aria-hidden="true" tabindex="-1"></a> ]<span class="op">,</span></span>
<span id="cb10-22"><a href="#cb10-22" aria-hidden="true" tabindex="-1"></a>}<span class="op">;</span></span>
<span id="cb10-23"><a href="#cb10-23" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidv4</span>(v4options)<span class="op">;</span> <span class="co">// ⇨ '109156be-c4fb-41ea-b1b4-efe1671c5836'</span></span></code></pre></div>
<h3 id="uuidv5name-namespace-buffer-offset">uuid.v5(name, namespace[,
buffer[, offset]])</h3>
<p>Create an RFC version 5 (namespace w/ SHA-1) UUID</p>
<table>
<tbody>
<tr class="odd">
<td><code>name</code></td>
<td><code>String | Array</code></td>
</tr>
<tr class="even">
<td><code>namespace</code></td>
<td><code>String | Array[16]</code> Namespace UUID</td>
</tr>
<tr class="odd">
<td>[<code>buffer</code>]</td>
<td><code>Array | Buffer</code> If specified, uuid will be written here
in byte-form, starting at <code>offset</code></td>
</tr>
<tr class="even">
<td>[<code>offset</code> = 0]</td>
<td><code>Number</code> Index to start writing UUID bytes in
<code>buffer</code></td>
</tr>
<tr class="odd">
<td><em>returns</em></td>
<td>UUID <code>String</code> if no <code>buffer</code> is specified,
otherwise returns <code>buffer</code></td>
</tr>
</tbody>
</table>
<p>Note: The RFC <code>DNS</code> and <code>URL</code> namespaces are
available as <code>v5.DNS</code> and <code>v5.URL</code>.</p>
<p>Example with custom namespace:</p>
<div class="sourceCode" id="cb11"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { v5 <span class="im">as</span> uuidv5 } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a><span class="co">// Define a custom namespace. Readers, create your own using something like</span></span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a><span class="co">// https://www.uuidgenerator.net/</span></span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> MY_NAMESPACE <span class="op">=</span> <span class="st">'1b671a64-40d5-491e-99b0-da01ff1f3341'</span><span class="op">;</span></span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidv5</span>(<span class="st">'Hello, World!'</span><span class="op">,</span> MY_NAMESPACE)<span class="op">;</span> <span class="co">// ⇨ '630eb68f-e0fa-5ecc-887a-7c7a62614681'</span></span></code></pre></div>
<p>Example with RFC <code>URL</code> namespace:</p>
<div class="sourceCode" id="cb12"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { v5 <span class="im">as</span> uuidv5 } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidv5</span>(<span class="st">'https://www.w3.org/'</span><span class="op">,</span> uuidv5<span class="op">.</span><span class="at">URL</span>)<span class="op">;</span> <span class="co">// ⇨ 'c106a26a-21bb-5538-8bf2-57095d1976c1'</span></span></code></pre></div>
<h3 id="uuidvalidatestr">uuid.validate(str)</h3>
<p>Test a string to see if it is a valid UUID</p>
<table>
<tbody>
<tr class="odd">
<td><code>str</code></td>
<td><code>String</code> to validate</td>
</tr>
<tr class="even">
<td><em>returns</em></td>
<td><code>true</code> if string is a valid UUID, <code>false</code>
otherwise</td>
</tr>
</tbody>
</table>
<p>Example:</p>
<div class="sourceCode" id="cb13"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { validate <span class="im">as</span> uuidValidate } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidValidate</span>(<span class="st">'not a UUID'</span>)<span class="op">;</span> <span class="co">// ⇨ false</span></span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidValidate</span>(<span class="st">'6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'</span>)<span class="op">;</span> <span class="co">// ⇨ true</span></span></code></pre></div>
<p>Using <code>validate</code> and <code>version</code> together it is
possible to do per-version validation, e.g. validate for only v4
UUIds.</p>
<div class="sourceCode" id="cb14"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { version <span class="im">as</span> uuidVersion } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { validate <span class="im">as</span> uuidValidate } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">uuidValidateV4</span>(uuid) {</span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="fu">uuidValidate</span>(uuid) <span class="op">&&</span> <span class="fu">uuidVersion</span>(uuid) <span class="op">===</span> <span class="dv">4</span><span class="op">;</span></span>
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-8"><a href="#cb14-8" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> v1Uuid <span class="op">=</span> <span class="st">'d9428888-122b-11e1-b85c-61cd3cbb3210'</span><span class="op">;</span></span>
<span id="cb14-9"><a href="#cb14-9" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> v4Uuid <span class="op">=</span> <span class="st">'109156be-c4fb-41ea-b1b4-efe1671c5836'</span><span class="op">;</span></span>
<span id="cb14-10"><a href="#cb14-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-11"><a href="#cb14-11" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidValidateV4</span>(v4Uuid)<span class="op">;</span> <span class="co">// ⇨ true</span></span>
<span id="cb14-12"><a href="#cb14-12" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidValidateV4</span>(v1Uuid)<span class="op">;</span> <span class="co">// ⇨ false</span></span></code></pre></div>
<h3 id="uuidversionstr">uuid.version(str)</h3>
<p>Detect RFC version of a UUID</p>
<table>
<tbody>
<tr class="odd">
<td><code>str</code></td>
<td>A valid UUID <code>String</code></td>
</tr>
<tr class="even">
<td><em>returns</em></td>
<td><code>Number</code> The RFC version of the UUID</td>
</tr>
<tr class="odd">
<td><em>throws</em></td>
<td><code>TypeError</code> if <code>str</code> is not a valid UUID</td>
</tr>
</tbody>
</table>
<p>Example:</p>
<div class="sourceCode" id="cb15"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { version <span class="im">as</span> uuidVersion } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidVersion</span>(<span class="st">'45637ec4-c85f-11ea-87d0-0242ac130003'</span>)<span class="op">;</span> <span class="co">// ⇨ 1</span></span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidVersion</span>(<span class="st">'6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'</span>)<span class="op">;</span> <span class="co">// ⇨ 4</span></span></code></pre></div>
<h2 id="command-line">Command Line</h2>
<p>UUIDs can be generated from the command line using
<code>uuid</code>.</p>
<pre class="shell"><code>$ uuid
ddeb27fb-d9a0-4624-be4d-4615062daed4</code></pre>
<p>The default is to generate version 4 UUIDS, however the other
versions are supported. Type <code>uuid --help</code> for details:</p>
<pre class="shell"><code>$ uuid --help
Usage:
uuid
uuid v1
uuid v3 <name> <namespace uuid>
uuid v4
uuid v5 <name> <namespace uuid>
uuid --help
Note: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs
defined by RFC4122</code></pre>
<h2 id="ecmascript-modules">ECMAScript Modules</h2>
<p>This library comes with <a
href="https://www.ecma-international.org/ecma-262/6.0/#sec-modules">ECMAScript
Modules</a> (ESM) support for Node.js versions that support it (<a
href="./examples/node-esmodules/">example</a>) as well as bundlers like
<a href="https://rollupjs.org/guide/en/#tree-shaking">rollup.js</a> (<a
href="./examples/browser-rollup/">example</a>) and <a
href="https://webpack.js.org/guides/tree-shaking/">webpack</a> (<a
href="./examples/browser-webpack/">example</a>) (targeting both, Node.js
and browser environments).</p>
<div class="sourceCode" id="cb18"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { v4 <span class="im">as</span> uuidv4 } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidv4</span>()<span class="op">;</span> <span class="co">// ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'</span></span></code></pre></div>
<p>To run the examples you must first create a dist build of this
library in the module root:</p>
<pre class="shell"><code>npm run build</code></pre>
<h2 id="cdn-builds">CDN Builds</h2>
<h3 id="ecmascript-modules-1">ECMAScript Modules</h3>
<p>To load this module directly into modern browsers that <a
href="https://caniuse.com/#feat=es6-module">support loading ECMAScript
Modules</a> you can make use of <a
href="https://jspm.org/">jspm</a>:</p>
<div class="sourceCode" id="cb20"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><script</span><span class="ot"> type=</span><span class="st">"module"</span><span class="kw">></span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a> <span class="im">import</span> { v4 <span class="im">as</span> uuidv4 } <span class="im">from</span> <span class="st">'https://jspm.dev/uuid'</span><span class="op">;</span></span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="fu">uuidv4</span>())<span class="op">;</span> <span class="co">// ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'</span></span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a><span class="kw"></script></span></span></code></pre></div>
<h3 id="umd">UMD</h3>
<p>To load this module directly into older browsers you can use the <a
href="https://github.com/umdjs/umd">UMD (Universal Module
Definition)</a> builds from any of the following CDNs:</p>
<p><strong>Using <a
href="https://unpkg.com/uuid@latest/dist/umd/">UNPKG</a></strong>:</p>
<div class="sourceCode" id="cb21"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><script</span> <span class="er">src</span><span class="ot">=</span><span class="st">"https://unpkg.com/uuid@latest/dist/umd/uuidv4.min.js"</span><span class="kw">></script></span></span></code></pre></div>
<p><strong>Using <a
href="https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/">jsDelivr</a></strong>:</p>
<div class="sourceCode" id="cb22"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><script</span> <span class="er">src</span><span class="ot">=</span><span class="st">"https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/uuidv4.min.js"</span><span class="kw">></script></span></span></code></pre></div>
<p><strong>Using <a
href="https://cdnjs.com/libraries/uuid">cdnjs</a></strong>:</p>
<div class="sourceCode" id="cb23"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><script</span> <span class="er">src</span><span class="ot">=</span><span class="st">"https://cdnjs.cloudflare.com/ajax/libs/uuid/8.1.0/uuidv4.min.js"</span><span class="kw">></script></span></span></code></pre></div>
<p>These CDNs all provide the same <a
href="#uuidv4options-buffer-offset"><code>uuidv4()</code></a>
method:</p>
<div class="sourceCode" id="cb24"><pre
class="sourceCode html"><code class="sourceCode html"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><script></span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">uuidv4</span>()<span class="op">;</span> <span class="co">// ⇨ '55af1e37-0734-46d8-b070-a1e42e4fc392'</span></span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a><span class="kw"></script></span></span></code></pre></div>
<p>Methods for the other algorithms (<a
href="#uuidv1options-buffer-offset"><code>uuidv1()</code></a>, <a
href="#uuidv3name-namespace-buffer-offset"><code>uuidv3()</code></a> and
<a href="#uuidv5name-namespace-buffer-offset"><code>uuidv5()</code></a>)
are available from the files <code>uuidv1.min.js</code>,
<code>uuidv3.min.js</code> and <code>uuidv5.min.js</code>
respectively.</p>
<h2 id="getrandomvalues-not-supported">"getRandomValues() not
supported"</h2>
<p>This error occurs in environments where the standard <a
href="https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues"><code>crypto.getRandomValues()</code></a>
API is not supported. This issue can be resolved by adding an
appropriate polyfill:</p>
<h3 id="react-native--expo">React Native / Expo</h3>
<ol type="1">
<li>Install <a
href="https://github.com/LinusU/react-native-get-random-values#readme"><code>react-native-get-random-values</code></a></li>
<li>Import it <em>before</em> <code>uuid</code>. Since <code>uuid</code>
might also appear as a transitive dependency of some other imports it's
safest to just import <code>react-native-get-random-values</code> as the
very first thing in your entry point:</li>
</ol>
<div class="sourceCode" id="cb25"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> <span class="st">'react-native-get-random-values'</span><span class="op">;</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { v4 <span class="im">as</span> uuidv4 } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span></code></pre></div>
<p>Note: If you are using Expo, you must be using at least
<code>react-native-get-random-values@1.5.0</code> and
<code>expo@39.0.0</code>.</p>
<h3 id="web-workers--service-workers-edge--18">Web Workers / Service
Workers (Edge <= 18)</h3>
<p><a href="https://caniuse.com/#feat=cryptography">In Edge <= 18,
Web Crypto is not supported in Web Workers or Service Workers</a> and we
are not aware of a polyfill (let us know if you find one, please).</p>
<h2 id="upgrading-from-uuid7x">Upgrading From <code>uuid@7.x</code></h2>
<h3 id="only-named-exports-supported-when-using-with-nodejs-esm">Only
Named Exports Supported When Using with Node.js ESM</h3>
<p><code>uuid@7.x</code> 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.</p>
<p>Instead of doing:</p>
<div class="sourceCode" id="cb26"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> uuid <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a>uuid<span class="op">.</span><span class="fu">v4</span>()<span class="op">;</span></span></code></pre></div>
<p>you will now have to use the named exports:</p>
<div class="sourceCode" id="cb27"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { v4 <span class="im">as</span> uuidv4 } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidv4</span>()<span class="op">;</span></span></code></pre></div>
<h3 id="deep-requires-no-longer-supported">Deep Requires No Longer
Supported</h3>
<p>Deep requires like <code>require('uuid/v4')</code> <a
href="#deep-requires-now-deprecated">which have been deprecated in
<code>uuid@7.x</code></a> are no longer supported.</p>
<h2 id="upgrading-from-uuid3x">Upgrading From <code>uuid@3.x</code></h2>
<p>"<em>Wait... what happened to <code>uuid@4.x</code> -
<code>uuid@6.x</code>?!?</em>"</p>
<p>In order to avoid confusion with RFC <a
href="#uuidv4options-buffer-offset">version 4</a> and <a
href="#uuidv5name-namespace-buffer-offset">version 5</a> UUIDs, and a
possible <a href="http://gh.peabody.io/uuidv6/">version 6</a>, releases
4 thru 6 of this module have been skipped.</p>
<h3 id="deep-requires-now-deprecated">Deep Requires Now Deprecated</h3>
<p><code>uuid@3.x</code> encouraged the use of deep requires to minimize
the bundle size of browser builds:</p>
<div class="sourceCode" id="cb28"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> uuidv4 <span class="op">=</span> <span class="pp">require</span>(<span class="st">'uuid/v4'</span>)<span class="op">;</span> <span class="co">// <== NOW </span><span class="al">DEPRECATED</span><span class="co">!</span></span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidv4</span>()<span class="op">;</span></span></code></pre></div>
<p>As of <code>uuid@7.x</code> 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 <code>import</code>
syntax:</p>
<div class="sourceCode" id="cb29"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> { v4 <span class="im">as</span> uuidv4 } <span class="im">from</span> <span class="st">'uuid'</span><span class="op">;</span></span>
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidv4</span>()<span class="op">;</span></span></code></pre></div>
<p>... or for CommonJS:</p>
<div class="sourceCode" id="cb30"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> { <span class="dt">v4</span><span class="op">:</span> uuidv4 } <span class="op">=</span> <span class="pp">require</span>(<span class="st">'uuid'</span>)<span class="op">;</span></span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a><span class="fu">uuidv4</span>()<span class="op">;</span></span></code></pre></div>
<h3 id="default-export-removed">Default Export Removed</h3>
<p><code>uuid@3.x</code> was exporting the Version 4 UUID method as a
default export:</p>
<div class="sourceCode" id="cb31"><pre
class="sourceCode javascript"><code class="sourceCode javascript"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="kw">const</span> uuid <span class="op">=</span> <span class="pp">require</span>(<span class="st">'uuid'</span>)<span class="op">;</span> <span class="co">// <== REMOVED!</span></span></code></pre></div>
<p>This usage pattern was already discouraged in <code>uuid@3.x</code>
and has been removed in <code>uuid@7.x</code>.</p>
<hr />
<p>Markdown generated from <a href="README_js.md">README_js.md</a> by <a
href="https://github.com/broofa/runmd"><img
src="http://i.imgur.com/h0FVyzU.png" alt="RunMD Logo" /></a></p>
</body>
</html>
|