UNPKG

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