UNPKG

12.8 kBMarkdownView Raw
1<!--
2 -- This file is auto-generated from README_js.md. Changes should be made there.
3 -->
4
5# uuid [![Build Status](https://github.com/uuidjs/uuid/workflows/CI/badge.svg)](https://github.com/uuidjs/uuid/actions)
6
7For the creation of [RFC4122](http://www.ietf.org/rfc/rfc4122.txt) UUIDs
8
9- **Complete** - Support for RFC4122 version 1, 3, 4, and 5 UUIDs
10- **Cross-platform** - Support for ...
11 - CommonJS, [ECMAScript Modules](#ecmascript-modules) and UMD builds
12 - Node 8, 10, 12
13 - Chrome, Safari, Firefox, Edge, IE 11 browsers
14 - Webpack and rollup.js module bundlers
15 - [React Native](#react-native)
16- **Secure** - Cryptographically-strong random values
17- **Small** - Zero-dependency, small footprint, plays nice with "tree shaking" packagers
18- **CLI** - Includes the [`uuid` command line](#command-line) utility
19
20**Upgrading from uuid\@3?** Your code is probably okay, but check out [Upgrading
21From uuid\@3](#upgrading-from-uuid3) for details.
22
23## Quickstart - Node.js/CommonJS
24
25```shell
26npm install uuid
27```
28
29Once installed, decide which type of UUID you need. RFC4122 provides for four
30versions, all of which are supported here. In order of popularity, they are:
31
32- Version 4 (random) - Created from cryptographically-strong random values
33- Version 1 (timestamp) - Created from the system clock (plus random values)
34- Version 5 (namespace, SHA-1) - Created from user-supplied name and namespace strings
35- Version 3 (namespace, MD5) - Like version 5, above, but with a poorer hash algorithm
36
37**Unsure which one to use?** Use version 4 (random) unless you have a specific need for one of the other versions. See also [this FAQ](https://github.com/tc39/proposal-uuid#faq).
38
39### Create Version 4 (Random) UUIDs
40
41```javascript
42import { v4 as uuidv4 } from 'uuid';
43uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
44```
45
46### Create Version 1 (Timestamp) UUIDs
47
48```javascript
49import { v1 as uuidv1 } from 'uuid';
50uuidv1(); // ⇨ '2c5ea4c0-4067-11e9-8b2d-1b9d6bcdbbfd'
51```
52
53### Create Version 3 or Version 5 (Namespace) UUIDs
54
55&#x26a0;&#xfe0f; Version 3 and Version 5 UUIDs are basically the same, differing
56only in the underlying hash algorithm. Note that per the RFC, "_If backward
57compatibility is not an issue, SHA-1 [Version 5] is preferred_."
58
59&#x26a0;&#xfe0f; If using a custom namespace **be sure to generate your own
60namespace UUID**. You can grab one [here](https://www.uuidgenerator.net/).
61
62```javascript
63import { v5 as uuidv5 } from 'uuid'; // For version 5
64import { v3 as uuidv3 } from 'uuid'; // For version 3
65
66// Using predefined DNS namespace (for domain names)
67uuidv5('hello.example.com', uuidv5.DNS); // ⇨ 'fdda765f-fc57-5604-a269-52a7df8164ec'
68uuidv3('hello.example.com', uuidv3.DNS); // ⇨ '9125a8dc-52ee-365b-a5aa-81b0b3681cf6'
69
70// Using predefined URL namespace (for URLs)
71uuidv5('http://example.com/hello', uuidv5.URL); // ⇨ '3bbcee75-cecc-5b56-8031-b6641c1ed1f1'
72uuidv3('http://example.com/hello', uuidv3.URL); // ⇨ 'c6235813-3ba4-3801-ae84-e0a6ebb7d138'
73
74// Using a custom namespace (See note, above, about generating your own
75// namespace UUID)
76const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
77uuidv5('Hello, World!', MY_NAMESPACE); // ⇨ '630eb68f-e0fa-5ecc-887a-7c7a62614681'
78uuidv3('Hello, World!', MY_NAMESPACE); // ⇨ 'e8b5a51d-11c8-3310-a6ab-367563f20686'
79```
80
81## API
82
83### Version 4 (Random)
84
85```javascript
86import { v4 as uuidv4 } from 'uuid';
87
88// Incantations
89uuidv4();
90uuidv4(options);
91uuidv4(options, buffer, offset);
92```
93
94Generate and return a RFC4122 version 4 UUID.
95
96- `options` - (Object) Optional uuid state to apply. Properties may include:
97 - `random` - (Number[16]) Array of 16 numbers (0-255) to use in place of randomly generated values. Takes precedence over `options.rng`.
98 - `rng` - (Function) Random # generator function that returns an Array[16] of byte values (0-255). Alternative to `options.random`.
99- `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
100- `offset` - (Number) Starting index in `buffer` at which to begin writing.
101
102Returns `buffer`, if specified, otherwise the string form of the UUID
103
104Example: Generate string UUID with predefined `random` values
105
106```javascript
107const v4options = {
108 random: [
109 0x10,
110 0x91,
111 0x56,
112 0xbe,
113 0xc4,
114 0xfb,
115 0xc1,
116 0xea,
117 0x71,
118 0xb4,
119 0xef,
120 0xe1,
121 0x67,
122 0x1c,
123 0x58,
124 0x36,
125 ],
126};
127uuidv4(v4options); // ⇨ '109156be-c4fb-41ea-b1b4-efe1671c5836'
128```
129
130Example: Generate two IDs in a single buffer
131
132```javascript
133const buffer = new Array();
134uuidv4(null, buffer, 0); // ⇨
135 // [
136 // 155, 29, 235, 77, 59,
137 // 125, 75, 173, 155, 221,
138 // 43, 13, 123, 61, 203,
139 // 109
140 // ]
141uuidv4(null, buffer, 16); // ⇨
142 // [
143 // 155, 29, 235, 77, 59, 125, 75, 173,
144 // 155, 221, 43, 13, 123, 61, 203, 109,
145 // 27, 157, 107, 205, 187, 253, 75, 45,
146 // 155, 93, 171, 141, 251, 189, 75, 237
147 // ]
148```
149
150### Version 1 (Timestamp)
151
152```javascript
153import { v1 as uuidv1 } from 'uuid';
154
155// Incantations
156uuidv1();
157uuidv1(options);
158uuidv1(options, buffer, offset);
159```
160
161Generate and return a RFC4122 version 1 (timestamp) UUID.
162
163- `options` - (Object) Optional uuid state to apply. Properties may include:
164 - `node` - (Array) Node id as Array of 6 bytes (per 4.1.6). Default: Randomly generated ID. See note 1.
165 - `clockseq` - (Number between 0 - 0x3fff) RFC clock sequence. Default: An internally maintained clockseq is used.
166 - `msecs` - (Number) Time in milliseconds since unix Epoch. Default: The current time is used.
167 - `nsecs` - (Number between 0-9999) additional time, in 100-nanosecond units. Ignored if `msecs` is unspecified. Default: internal uuid counter is used, as per 4.2.1.2.
168 - `random` - (Number[16]) Array of 16 numbers (0-255) to use for initialization of `node` and `clockseq` as described above. Takes precedence over `options.rng`.
169 - `rng` - (Function) Random # generator function that returns an Array[16] of byte values (0-255). Alternative to `options.random`.
170- `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
171- `offset` - (Number) Starting index in `buffer` at which to begin writing.
172
173Returns `buffer`, if specified, otherwise the string form of the UUID
174
175Note: The default [node id](https://tools.ietf.org/html/rfc4122#section-4.1.6) (the last 12 digits in the UUID) is generated once, randomly, on process startup, and then remains unchanged for the duration of the process.
176
177Example: Generate string UUID with fully-specified options
178
179```javascript
180const v1options = {
181 node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
182 clockseq: 0x1234,
183 msecs: new Date('2011-11-01').getTime(),
184 nsecs: 5678,
185};
186uuidv1(v1options); // ⇨ '710b962e-041c-11e1-9234-0123456789ab'
187```
188
189Example: In-place generation of two binary IDs
190
191```javascript
192// Generate two ids in an array
193const arr = new Array();
194uuidv1(null, arr, 0); // ⇨
195 // [
196 // 44, 94, 164, 192, 64,
197 // 103, 17, 233, 146, 52,
198 // 27, 157, 107, 205, 187,
199 // 253
200 // ]
201uuidv1(null, arr, 16); // ⇨
202 // [
203 // 44, 94, 164, 192, 64, 103, 17, 233,
204 // 146, 52, 27, 157, 107, 205, 187, 253,
205 // 44, 94, 164, 193, 64, 103, 17, 233,
206 // 146, 52, 27, 157, 107, 205, 187, 253
207 // ]
208```
209
210### Version 5 (Namespace)
211
212```javascript
213import { v5 as uuidv5 } from 'uuid';
214
215// Incantations
216uuidv5(name, namespace);
217uuidv5(name, namespace, buffer);
218uuidv5(name, namespace, buffer, offset);
219```
220
221Generate and return a RFC4122 version 5 UUID.
222
223- `name` - (String | Array[]) "name" to create UUID with
224- `namespace` - (String | Array[]) "namespace" UUID either as a String or Array[16] of byte values
225- `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
226- `offset` - (Number) Starting index in `buffer` at which to begin writing. Default = 0
227
228Returns `buffer`, if specified, otherwise the string form of the UUID
229
230Example:
231
232```javascript
233uuidv5('hello world', MY_NAMESPACE); // ⇨ '9f282611-e0fd-5650-8953-89c8e342da0b'
234```
235
236### Version 3 (Namespace)
237
238&#x26a0;&#xfe0f; Note: Per the RFC, "_If backward compatibility is not an issue, SHA-1 [Version 5] is preferred_."
239
240```javascript
241import { v3 as uuidv3 } from 'uuid';
242
243// Incantations
244uuidv3(name, namespace);
245uuidv3(name, namespace, buffer);
246uuidv3(name, namespace, buffer, offset);
247```
248
249Generate and return a RFC4122 version 3 UUID.
250
251- `name` - (String | Array[]) "name" to create UUID with
252- `namespace` - (String | Array[]) "namespace" UUID either as a String or Array[16] of byte values
253- `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
254- `offset` - (Number) Starting index in `buffer` at which to begin writing. Default = 0
255
256Returns `buffer`, if specified, otherwise the string form of the UUID
257
258Example:
259
260```javascript
261uuidv3('hello world', MY_NAMESPACE); // ⇨ '042ffd34-d989-321c-ad06-f60826172424'
262```
263
264## Command Line
265
266UUIDs can be generated from the command line using `uuid`.
267
268```shell
269$ uuid
270ddeb27fb-d9a0-4624-be4d-4615062daed4
271```
272
273The default is to generate version 4 UUIDS, however the other versions are supported. Type `uuid --help` for details:
274
275```
276$ uuid --help
277
278Usage:
279 uuid
280 uuid v1
281 uuid v3 <name> <namespace uuid>
282 uuid v4
283 uuid v5 <name> <namespace uuid>
284 uuid --help
285
286Note: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs
287defined by RFC4122
288```
289
290## ECMAScript Modules
291
292For usage in the browser `uuid` provides support for [ECMAScript
293Modules](https://www.ecma-international.org/ecma-262/6.0/#sec-modules) (ESM) that enable
294tree-shaking for bundlers, like [rollup.js](https://rollupjs.org/guide/en/#tree-shaking)
295([example](./examples/browser-rollup/)) and [webpack](https://webpack.js.org/guides/tree-shaking/)
296([example](./examples/browser-webpack/)).
297
298```javascript
299import { v4 as uuidv4 } from 'uuid';
300uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
301```
302
303There is experimental native ESM support for [the browser](./examples/browser-esmodules/) but it
304should not be considered ready for production use and may change or disappear in future releases.
305
306To run the examples you must first create a dist build of this library in the module root:
307
308```
309npm run build
310```
311
312## UMD Builds
313
314If you want to load a minified UMD build directly in the browser you can use one of the popular npm
315CDNs:
316
317```html
318<script src="https://unpkg.com/uuid@latest/dist/umd/uuidv4.min.js"></script>
319<script>
320 alert(uuidv4());
321</script>
322```
323
324or
325
326```html
327<script src="https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/uuidv4.min.js"></script>
328<script>
329 alert(uuidv4());
330</script>
331```
332
333Available bundles:
334
335- https://unpkg.com/uuid@latest/dist/umd/
336- https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/
337
338## "getRandomValues() not supported"
339
340This error occurs in environments where the standard
341[`crypto.getRandomValues()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues)
342API is not supported. This issue can be resolved by adding an appropriate polyfill:
343
344### React Native
345
3461. Install [`react-native-get-random-values`](https://github.com/LinusU/react-native-get-random-values#readme)
3471. Import it before `uuid`:
348
349```javascript
350import 'react-native-get-random-values';
351import { v4 as uuidv4 } from 'uuid';
352```
353
354### Web Workers / Service Workers (Edge <= 18)
355
356[In Edge <= 18, Web Crypto is not supported in Web Workers or Service
357Workers](https://caniuse.com/#feat=cryptography) and we are not aware of a polyfill (let us know if
358you find one, please).
359
360## Upgrading From uuid\@3
361
362"_Wait... what happened to uuid\@4 - uuid\@6?!?_"
363
364In order to avoid confusion with RFC [version 4](#version-4-random) and [version
3655](#version-5-namespace) UUIDs, and a possible [version
3666](http://gh.peabody.io/uuidv6/), releases 4 thru 6 of this module have been
367skipped. Hence, how we're now at uuid\@7.
368
369### Deep Requires Now Deprecated
370
371uuid\@3 encouraged the use of deep requires to minimize the bundle size of
372browser builds:
373
374```javascript
375const uuidv4 = require('uuid/v4'); // <== NOW DEPRECATED!
376uuidv4();
377```
378
379As of uuid\@7 this library now provides ECMAScript modules builds, which allow
380packagers like Webpack and Rollup to do "tree-shaking" to remove dead code.
381Instead, use the `import` syntax:
382
383```javascript
384import { v4 as uuidv4 } from 'uuid';
385uuidv4();
386```
387
388... or for CommonJS:
389
390```javascript
391const { v4: uuidv4 } = require('uuid');
392uuidv4();
393```
394
395### Default Export Removed
396
397uuid\@3 was exporting the Version 4 UUID method as a default export:
398
399```javascript
400const uuid = require('uuid'); // <== REMOVED!
401```
402
403This usage pattern was already discouraged in uuid\@3 and has been removed in uuid\@7.
404
405----
406Markdown generated from [README_js.md](README_js.md) by [![RunMD Logo](http://i.imgur.com/h0FVyzU.png)](https://github.com/broofa/runmd)
\No newline at end of file