UNPKG

13.5 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, 14
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
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
41ECMAScript Module syntax:
42
43```javascript
44import { v4 as uuidv4 } from 'uuid';
45uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
46```
47
48CommonJS syntax:
49
50```javascript
51const { v4: uuidv4 } = require('uuid');
52uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
53```
54
55### Create Version 1 (Timestamp) UUIDs
56
57```javascript
58import { v1 as uuidv1 } from 'uuid';
59uuidv1(); // ⇨ '2c5ea4c0-4067-11e9-8bad-9b1deb4d3b7d'
60```
61
62### Create Version 3 or Version 5 (Namespace) UUIDs
63
64&#x26a0;&#xfe0f; Version 3 and Version 5 UUIDs are basically the same, differing
65only in the underlying hash algorithm. Note that per the RFC, "_If backward
66compatibility is not an issue, SHA-1 [Version 5] is preferred_."
67
68&#x26a0;&#xfe0f; If using a custom namespace **be sure to generate your own
69namespace UUID**. You can grab one [here](https://www.uuidgenerator.net/).
70
71```javascript
72import { v5 as uuidv5 } from 'uuid'; // For version 5
73import { v3 as uuidv3 } from 'uuid'; // For version 3
74
75// Using predefined DNS namespace (for domain names)
76uuidv5('hello.example.com', uuidv5.DNS); // ⇨ 'fdda765f-fc57-5604-a269-52a7df8164ec'
77uuidv3('hello.example.com', uuidv3.DNS); // ⇨ '9125a8dc-52ee-365b-a5aa-81b0b3681cf6'
78
79// Using predefined URL namespace (for URLs)
80uuidv5('http://example.com/hello', uuidv5.URL); // ⇨ '3bbcee75-cecc-5b56-8031-b6641c1ed1f1'
81uuidv3('http://example.com/hello', uuidv3.URL); // ⇨ 'c6235813-3ba4-3801-ae84-e0a6ebb7d138'
82
83// Using a custom namespace (See note, above, about generating your own
84// namespace UUID)
85const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
86uuidv5('Hello, World!', MY_NAMESPACE); // ⇨ '630eb68f-e0fa-5ecc-887a-7c7a62614681'
87uuidv3('Hello, World!', MY_NAMESPACE); // ⇨ 'e8b5a51d-11c8-3310-a6ab-367563f20686'
88```
89
90## API
91
92### Version 4 (Random)
93
94```javascript
95import { v4 as uuidv4 } from 'uuid';
96
97// Incantations
98uuidv4();
99uuidv4(options);
100uuidv4(options, buffer, offset);
101```
102
103Generate and return a RFC4122 version 4 UUID.
104
105- `options` - (Object) Optional uuid state to apply. Properties may include:
106 - `random` - (Number[16]) Array of 16 numbers (0-255) to use in place of randomly generated values. Takes precedence over `options.rng`.
107 - `rng` - (Function) Random # generator function that returns an Array[16] of byte values (0-255). Alternative to `options.random`.
108- `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
109- `offset` - (Number) Starting index in `buffer` at which to begin writing.
110
111Returns `buffer`, if specified, otherwise the string form of the UUID
112
113Example: Generate string UUID with predefined `random` values
114
115```javascript
116const v4options = {
117 random: [
118 0x10,
119 0x91,
120 0x56,
121 0xbe,
122 0xc4,
123 0xfb,
124 0xc1,
125 0xea,
126 0x71,
127 0xb4,
128 0xef,
129 0xe1,
130 0x67,
131 0x1c,
132 0x58,
133 0x36,
134 ],
135};
136uuidv4(v4options); // ⇨ '109156be-c4fb-41ea-b1b4-efe1671c5836'
137```
138
139Example: Generate two IDs in a single buffer
140
141```javascript
142const buffer = new Array();
143uuidv4(null, buffer, 0); // ⇨
144 // [
145 // 27, 157, 107, 205, 187,
146 // 253, 75, 45, 155, 93,
147 // 171, 141, 251, 189, 75,
148 // 237
149 // ]
150uuidv4(null, buffer, 16); // ⇨
151 // [
152 // 27, 157, 107, 205, 187, 253, 75, 45,
153 // 155, 93, 171, 141, 251, 189, 75, 237,
154 // 155, 29, 235, 77, 59, 125, 75, 173,
155 // 155, 221, 43, 13, 123, 61, 203, 109
156 // ]
157```
158
159### Version 1 (Timestamp)
160
161```javascript
162import { v1 as uuidv1 } from 'uuid';
163
164// Incantations
165uuidv1();
166uuidv1(options);
167uuidv1(options, buffer, offset);
168```
169
170Generate and return a RFC4122 version 1 (timestamp) UUID.
171
172- `options` - (Object) Optional uuid state to apply. Properties may include:
173 - `node` - (Array) Node id as Array of 6 bytes (per 4.1.6). Default: Randomly generated ID. See note 1.
174 - `clockseq` - (Number between 0 - 0x3fff) RFC clock sequence. Default: An internally maintained clockseq is used.
175 - `msecs` - (Number) Time in milliseconds since unix Epoch. Default: The current time is used.
176 - `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.
177 - `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`.
178 - `rng` - (Function) Random # generator function that returns an Array[16] of byte values (0-255). Alternative to `options.random`.
179- `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
180- `offset` - (Number) Starting index in `buffer` at which to begin writing.
181
182Returns `buffer`, if specified, otherwise the string form of the UUID
183
184Note: 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.
185
186Example: Generate string UUID with fully-specified options
187
188```javascript
189const v1options = {
190 node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
191 clockseq: 0x1234,
192 msecs: new Date('2011-11-01').getTime(),
193 nsecs: 5678,
194};
195uuidv1(v1options); // ⇨ '710b962e-041c-11e1-9234-0123456789ab'
196```
197
198Example: In-place generation of two binary IDs
199
200```javascript
201// Generate two ids in an array
202const arr = new Array();
203uuidv1(null, arr, 0); // ⇨
204 // [
205 // 44, 94, 164, 192, 64, 103,
206 // 17, 233, 146, 52, 155, 29,
207 // 235, 77, 59, 125
208 // ]
209uuidv1(null, arr, 16); // ⇨
210 // [
211 // 44, 94, 164, 192, 64, 103, 17, 233,
212 // 146, 52, 155, 29, 235, 77, 59, 125,
213 // 44, 94, 164, 193, 64, 103, 17, 233,
214 // 146, 52, 155, 29, 235, 77, 59, 125
215 // ]
216```
217
218### Version 5 (Namespace)
219
220```javascript
221import { v5 as uuidv5 } from 'uuid';
222
223// Incantations
224uuidv5(name, namespace);
225uuidv5(name, namespace, buffer);
226uuidv5(name, namespace, buffer, offset);
227```
228
229Generate and return a RFC4122 version 5 UUID.
230
231- `name` - (String | Array[]) "name" to create UUID with
232- `namespace` - (String | Array[]) "namespace" UUID either as a String or Array[16] of byte values
233- `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
234- `offset` - (Number) Starting index in `buffer` at which to begin writing. Default = 0
235
236Returns `buffer`, if specified, otherwise the string form of the UUID
237
238Example:
239
240```javascript
241uuidv5('hello world', MY_NAMESPACE); // ⇨ '9f282611-e0fd-5650-8953-89c8e342da0b'
242```
243
244### Version 3 (Namespace)
245
246&#x26a0;&#xfe0f; Note: Per the RFC, "_If backward compatibility is not an issue, SHA-1 [Version 5] is preferred_."
247
248```javascript
249import { v3 as uuidv3 } from 'uuid';
250
251// Incantations
252uuidv3(name, namespace);
253uuidv3(name, namespace, buffer);
254uuidv3(name, namespace, buffer, offset);
255```
256
257Generate and return a RFC4122 version 3 UUID.
258
259- `name` - (String | Array[]) "name" to create UUID with
260- `namespace` - (String | Array[]) "namespace" UUID either as a String or Array[16] of byte values
261- `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
262- `offset` - (Number) Starting index in `buffer` at which to begin writing. Default = 0
263
264Returns `buffer`, if specified, otherwise the string form of the UUID
265
266Example:
267
268```javascript
269uuidv3('hello world', MY_NAMESPACE); // ⇨ '042ffd34-d989-321c-ad06-f60826172424'
270```
271
272## Command Line
273
274UUIDs can be generated from the command line using `uuid`.
275
276```shell
277$ uuid
278ddeb27fb-d9a0-4624-be4d-4615062daed4
279```
280
281The default is to generate version 4 UUIDS, however the other versions are supported. Type `uuid --help` for details:
282
283```
284$ uuid --help
285
286Usage:
287 uuid
288 uuid v1
289 uuid v3 <name> <namespace uuid>
290 uuid v4
291 uuid v5 <name> <namespace uuid>
292 uuid --help
293
294Note: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs
295defined by RFC4122
296```
297
298## ECMAScript Modules
299
300This library comes with [ECMAScript
301Modules](https://www.ecma-international.org/ecma-262/6.0/#sec-modules) (ESM) support for Node.js
302versions that support it ([example](./examples/node-esmodules/)) as well as bundlers like
303[rollup.js](https://rollupjs.org/guide/en/#tree-shaking) ([example](./examples/browser-rollup/))
304and [webpack](https://webpack.js.org/guides/tree-shaking/)
305([example](./examples/browser-webpack/)) (targeting both, Node.js and browser environments).
306
307```javascript
308import { v4 as uuidv4 } from 'uuid';
309uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
310```
311
312To run the examples you must first create a dist build of this library in the module root:
313
314```
315npm run build
316```
317
318## UMD Builds
319
320If you want to load a minified UMD build directly in the browser you can use one of the popular npm
321CDNs:
322
323```html
324<script src="https://unpkg.com/uuid@latest/dist/umd/uuidv4.min.js"></script>
325<script>
326 alert(uuidv4());
327</script>
328```
329
330or
331
332```html
333<script src="https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/uuidv4.min.js"></script>
334<script>
335 alert(uuidv4());
336</script>
337```
338
339Available bundles:
340
341- https://unpkg.com/uuid@latest/dist/umd/
342- https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/
343
344## "getRandomValues() not supported"
345
346This error occurs in environments where the standard
347[`crypto.getRandomValues()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues)
348API is not supported. This issue can be resolved by adding an appropriate polyfill:
349
350### React Native
351
3521. Install [`react-native-get-random-values`](https://github.com/LinusU/react-native-get-random-values#readme)
3531. Import it before `uuid`:
354
355```javascript
356import 'react-native-get-random-values';
357import { v4 as uuidv4 } from 'uuid';
358```
359
360### Web Workers / Service Workers (Edge <= 18)
361
362[In Edge <= 18, Web Crypto is not supported in Web Workers or Service
363Workers](https://caniuse.com/#feat=cryptography) and we are not aware of a polyfill (let us know if
364you find one, please).
365
366## Upgrading From uuid\@7
367
368### Only Named Exports Supported When Using with Node.js ESM
369
370uuid\@7 did not come with native ECMAScript Module (ESM) support for Node.js. Importing it in
371Node.js ESM consequently imported the CommonJS source with a default export. This library now comes
372with true Node.js ESM support and only provides named exports.
373
374Instead of doing:
375
376```javascript
377import uuid from 'uuid';
378uuid.v4();
379```
380
381you will now have to use the named exports:
382
383```javascript
384import { v4 as uuidv4 } from 'uuid';
385uuidv4();
386```
387
388### Deep Requires No Longer Supported
389
390Deep requires like `require('uuid/v4')` [which have been deprecated in
391uuid\@7](#deep-requires-now-deprecated) are no longer supported.
392
393## Upgrading From uuid\@3
394
395"_Wait... what happened to uuid\@4 - uuid\@6?!?_"
396
397In order to avoid confusion with RFC [version 4](#version-4-random) and [version
3985](#version-5-namespace) UUIDs, and a possible [version
3996](http://gh.peabody.io/uuidv6/), releases 4 thru 6 of this module have been
400skipped. Hence, how we're now at uuid\@7.
401
402### Deep Requires Now Deprecated
403
404uuid\@3 encouraged the use of deep requires to minimize the bundle size of
405browser builds:
406
407```javascript
408const uuidv4 = require('uuid/v4'); // <== NOW DEPRECATED!
409uuidv4();
410```
411
412As of uuid\@7 this library now provides ECMAScript modules builds, which allow
413packagers like Webpack and Rollup to do "tree-shaking" to remove dead code.
414Instead, use the `import` syntax:
415
416```javascript
417import { v4 as uuidv4 } from 'uuid';
418uuidv4();
419```
420
421... or for CommonJS:
422
423```javascript
424const { v4: uuidv4 } = require('uuid');
425uuidv4();
426```
427
428### Default Export Removed
429
430uuid\@3 was exporting the Version 4 UUID method as a default export:
431
432```javascript
433const uuid = require('uuid'); // <== REMOVED!
434```
435
436This usage pattern was already discouraged in uuid\@3 and has been removed in uuid\@7.
437
438----
439Markdown 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