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 |
|
7 | For 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
|
21 | From uuid\@3](#upgrading-from-uuid3) for details.
|
22 |
|
23 | ## Quickstart
|
24 |
|
25 | ```shell
|
26 | npm install uuid
|
27 | ```
|
28 |
|
29 | Once installed, decide which type of UUID you need. RFC4122 provides for four
|
30 | versions, 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 | ECMAScript Module syntax:
|
42 |
|
43 | ```javascript
|
44 | import { v4 as uuidv4 } from 'uuid';
|
45 | uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
|
46 | ```
|
47 |
|
48 | CommonJS syntax:
|
49 |
|
50 | ```javascript
|
51 | const { v4: uuidv4 } = require('uuid');
|
52 | uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
|
53 | ```
|
54 |
|
55 | ### Create Version 1 (Timestamp) UUIDs
|
56 |
|
57 | ```javascript
|
58 | import { v1 as uuidv1 } from 'uuid';
|
59 | uuidv1(); // ⇨ '2c5ea4c0-4067-11e9-8bad-9b1deb4d3b7d'
|
60 | ```
|
61 |
|
62 | ### Create Version 3 or Version 5 (Namespace) UUIDs
|
63 |
|
64 | ⚠️ Version 3 and Version 5 UUIDs are basically the same, differing
|
65 | only in the underlying hash algorithm. Note that per the RFC, "_If backward
|
66 | compatibility is not an issue, SHA-1 [Version 5] is preferred_."
|
67 |
|
68 | ⚠️ If using a custom namespace **be sure to generate your own
|
69 | namespace UUID**. You can grab one [here](https://www.uuidgenerator.net/).
|
70 |
|
71 | ```javascript
|
72 | import { v5 as uuidv5 } from 'uuid'; // For version 5
|
73 | import { v3 as uuidv3 } from 'uuid'; // For version 3
|
74 |
|
75 | // Using predefined DNS namespace (for domain names)
|
76 | uuidv5('hello.example.com', uuidv5.DNS); // ⇨ 'fdda765f-fc57-5604-a269-52a7df8164ec'
|
77 | uuidv3('hello.example.com', uuidv3.DNS); // ⇨ '9125a8dc-52ee-365b-a5aa-81b0b3681cf6'
|
78 |
|
79 | // Using predefined URL namespace (for URLs)
|
80 | uuidv5('http://example.com/hello', uuidv5.URL); // ⇨ '3bbcee75-cecc-5b56-8031-b6641c1ed1f1'
|
81 | uuidv3('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)
|
85 | const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
|
86 | uuidv5('Hello, World!', MY_NAMESPACE); // ⇨ '630eb68f-e0fa-5ecc-887a-7c7a62614681'
|
87 | uuidv3('Hello, World!', MY_NAMESPACE); // ⇨ 'e8b5a51d-11c8-3310-a6ab-367563f20686'
|
88 | ```
|
89 |
|
90 | ## API
|
91 |
|
92 | ### Version 4 (Random)
|
93 |
|
94 | ```javascript
|
95 | import { v4 as uuidv4 } from 'uuid';
|
96 |
|
97 | // Incantations
|
98 | uuidv4();
|
99 | uuidv4(options);
|
100 | uuidv4(options, buffer, offset);
|
101 | ```
|
102 |
|
103 | Generate 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 |
|
111 | Returns `buffer`, if specified, otherwise the string form of the UUID
|
112 |
|
113 | Example: Generate string UUID with predefined `random` values
|
114 |
|
115 | ```javascript
|
116 | const 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 | };
|
136 | uuidv4(v4options); // ⇨ '109156be-c4fb-41ea-b1b4-efe1671c5836'
|
137 | ```
|
138 |
|
139 | Example: Generate two IDs in a single buffer
|
140 |
|
141 | ```javascript
|
142 | const buffer = new Array();
|
143 | uuidv4(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 | // ]
|
150 | uuidv4(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
|
162 | import { v1 as uuidv1 } from 'uuid';
|
163 |
|
164 | // Incantations
|
165 | uuidv1();
|
166 | uuidv1(options);
|
167 | uuidv1(options, buffer, offset);
|
168 | ```
|
169 |
|
170 | Generate 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 |
|
182 | Returns `buffer`, if specified, otherwise the string form of the UUID
|
183 |
|
184 | Note: 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 |
|
186 | Example: Generate string UUID with fully-specified options
|
187 |
|
188 | ```javascript
|
189 | const v1options = {
|
190 | node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
|
191 | clockseq: 0x1234,
|
192 | msecs: new Date('2011-11-01').getTime(),
|
193 | nsecs: 5678,
|
194 | };
|
195 | uuidv1(v1options); // ⇨ '710b962e-041c-11e1-9234-0123456789ab'
|
196 | ```
|
197 |
|
198 | Example: In-place generation of two binary IDs
|
199 |
|
200 | ```javascript
|
201 | // Generate two ids in an array
|
202 | const arr = new Array();
|
203 | uuidv1(null, arr, 0); // ⇨
|
204 | // [
|
205 | // 44, 94, 164, 192, 64, 103,
|
206 | // 17, 233, 146, 52, 155, 29,
|
207 | // 235, 77, 59, 125
|
208 | // ]
|
209 | uuidv1(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
|
221 | import { v5 as uuidv5 } from 'uuid';
|
222 |
|
223 | // Incantations
|
224 | uuidv5(name, namespace);
|
225 | uuidv5(name, namespace, buffer);
|
226 | uuidv5(name, namespace, buffer, offset);
|
227 | ```
|
228 |
|
229 | Generate 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 |
|
236 | Returns `buffer`, if specified, otherwise the string form of the UUID
|
237 |
|
238 | Example:
|
239 |
|
240 | ```javascript
|
241 | uuidv5('hello world', MY_NAMESPACE); // ⇨ '9f282611-e0fd-5650-8953-89c8e342da0b'
|
242 | ```
|
243 |
|
244 | ### Version 3 (Namespace)
|
245 |
|
246 | ⚠️ Note: Per the RFC, "_If backward compatibility is not an issue, SHA-1 [Version 5] is preferred_."
|
247 |
|
248 | ```javascript
|
249 | import { v3 as uuidv3 } from 'uuid';
|
250 |
|
251 | // Incantations
|
252 | uuidv3(name, namespace);
|
253 | uuidv3(name, namespace, buffer);
|
254 | uuidv3(name, namespace, buffer, offset);
|
255 | ```
|
256 |
|
257 | Generate 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 |
|
264 | Returns `buffer`, if specified, otherwise the string form of the UUID
|
265 |
|
266 | Example:
|
267 |
|
268 | ```javascript
|
269 | uuidv3('hello world', MY_NAMESPACE); // ⇨ '042ffd34-d989-321c-ad06-f60826172424'
|
270 | ```
|
271 |
|
272 | ## Command Line
|
273 |
|
274 | UUIDs can be generated from the command line using `uuid`.
|
275 |
|
276 | ```shell
|
277 | $ uuid
|
278 | ddeb27fb-d9a0-4624-be4d-4615062daed4
|
279 | ```
|
280 |
|
281 | The default is to generate version 4 UUIDS, however the other versions are supported. Type `uuid --help` for details:
|
282 |
|
283 | ```
|
284 | $ uuid --help
|
285 |
|
286 | Usage:
|
287 | uuid
|
288 | uuid v1
|
289 | uuid v3 <name> <namespace uuid>
|
290 | uuid v4
|
291 | uuid v5 <name> <namespace uuid>
|
292 | uuid --help
|
293 |
|
294 | Note: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs
|
295 | defined by RFC4122
|
296 | ```
|
297 |
|
298 | ## ECMAScript Modules
|
299 |
|
300 | This library comes with [ECMAScript
|
301 | Modules](https://www.ecma-international.org/ecma-262/6.0/#sec-modules) (ESM) support for Node.js
|
302 | versions 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/))
|
304 | and [webpack](https://webpack.js.org/guides/tree-shaking/)
|
305 | ([example](./examples/browser-webpack/)) (targeting both, Node.js and browser environments).
|
306 |
|
307 | ```javascript
|
308 | import { v4 as uuidv4 } from 'uuid';
|
309 | uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
|
310 | ```
|
311 |
|
312 | To run the examples you must first create a dist build of this library in the module root:
|
313 |
|
314 | ```
|
315 | npm run build
|
316 | ```
|
317 |
|
318 | ## UMD Builds
|
319 |
|
320 | If you want to load a minified UMD build directly in the browser you can use one of the popular npm
|
321 | CDNs:
|
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 |
|
330 | or
|
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 |
|
339 | Available 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 |
|
346 | This error occurs in environments where the standard
|
347 | [`crypto.getRandomValues()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues)
|
348 | API is not supported. This issue can be resolved by adding an appropriate polyfill:
|
349 |
|
350 | ### React Native
|
351 |
|
352 | 1. Install [`react-native-get-random-values`](https://github.com/LinusU/react-native-get-random-values#readme)
|
353 | 1. Import it before `uuid`:
|
354 |
|
355 | ```javascript
|
356 | import 'react-native-get-random-values';
|
357 | import { 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
|
363 | Workers](https://caniuse.com/#feat=cryptography) and we are not aware of a polyfill (let us know if
|
364 | you find one, please).
|
365 |
|
366 | ## Upgrading From uuid\@7
|
367 |
|
368 | ### Only Named Exports Supported When Using with Node.js ESM
|
369 |
|
370 | uuid\@7 did not come with native ECMAScript Module (ESM) support for Node.js. Importing it in
|
371 | Node.js ESM consequently imported the CommonJS source with a default export. This library now comes
|
372 | with true Node.js ESM support and only provides named exports.
|
373 |
|
374 | Instead of doing:
|
375 |
|
376 | ```javascript
|
377 | import uuid from 'uuid';
|
378 | uuid.v4();
|
379 | ```
|
380 |
|
381 | you will now have to use the named exports:
|
382 |
|
383 | ```javascript
|
384 | import { v4 as uuidv4 } from 'uuid';
|
385 | uuidv4();
|
386 | ```
|
387 |
|
388 | ### Deep Requires No Longer Supported
|
389 |
|
390 | Deep requires like `require('uuid/v4')` [which have been deprecated in
|
391 | uuid\@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 |
|
397 | In order to avoid confusion with RFC [version 4](#version-4-random) and [version
|
398 | 5](#version-5-namespace) UUIDs, and a possible [version
|
399 | 6](http://gh.peabody.io/uuidv6/), releases 4 thru 6 of this module have been
|
400 | skipped. Hence, how we're now at uuid\@7.
|
401 |
|
402 | ### Deep Requires Now Deprecated
|
403 |
|
404 | uuid\@3 encouraged the use of deep requires to minimize the bundle size of
|
405 | browser builds:
|
406 |
|
407 | ```javascript
|
408 | const uuidv4 = require('uuid/v4'); // <== NOW DEPRECATED!
|
409 | uuidv4();
|
410 | ```
|
411 |
|
412 | As of uuid\@7 this library now provides ECMAScript modules builds, which allow
|
413 | packagers like Webpack and Rollup to do "tree-shaking" to remove dead code.
|
414 | Instead, use the `import` syntax:
|
415 |
|
416 | ```javascript
|
417 | import { v4 as uuidv4 } from 'uuid';
|
418 | uuidv4();
|
419 | ```
|
420 |
|
421 | ... or for CommonJS:
|
422 |
|
423 | ```javascript
|
424 | const { v4: uuidv4 } = require('uuid');
|
425 | uuidv4();
|
426 | ```
|
427 |
|
428 | ### Default Export Removed
|
429 |
|
430 | uuid\@3 was exporting the Version 4 UUID method as a default export:
|
431 |
|
432 | ```javascript
|
433 | const uuid = require('uuid'); // <== REMOVED!
|
434 | ```
|
435 |
|
436 | This usage pattern was already discouraged in uuid\@3 and has been removed in uuid\@7.
|
437 |
|
438 | ----
|
439 | Markdown 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 |