UNPKG

15.2 kBMarkdownView Raw
1<!--
2 -- This file is auto-generated from README_js.md. Changes should be made there.
3 -->
4
5# uuid [![CI](https://github.com/uuidjs/uuid/workflows/CI/badge.svg)](https://github.com/uuidjs/uuid/actions?query=workflow%3ACI) [![Browser](https://github.com/uuidjs/uuid/workflows/Browser/badge.svg)](https://github.com/uuidjs/uuid/actions?query=workflow%3ABrowser)
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.x`?** Your code is probably okay, but check out [Upgrading From `uuid@3.x`](#upgrading-from-uuid3x) for details.
21
22## Quickstart
23
24To create a random UUID...
25
26**1. Install**
27
28```shell
29npm install uuid
30```
31
32**2. Create a UUID** (ES6 module syntax)
33
34```javascript
35import { v4 as uuidv4 } from 'uuid';
36uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
37```
38
39... or using CommonJS syntax:
40
41```javascript
42const { v4: uuidv4 } = require('uuid');
43uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
44```
45
46For timestamp UUIDs, namespace UUIDs, and other options read on ...
47
48## API Summary
49
50| | | |
51| --- | --- | --- |
52| [`uuid.NIL`](#uuidnil) | The nil UUID string (all zeros) | New in `uuid@8.3` |
53| [`uuid.parse()`](#uuidparsestr) | Convert UUID string to array of bytes | New in `uuid@8.3` |
54| [`uuid.stringify()`](#uuidstringifyarr-offset) | Convert array of bytes to UUID string | New in `uuid@8.3` |
55| [`uuid.v1()`](#uuidv1options-buffer-offset) | Create a version 1 (timestamp) UUID | |
56| [`uuid.v3()`](#uuidv3name-namespace-buffer-offset) | Create a version 3 (namespace w/ MD5) UUID | |
57| [`uuid.v4()`](#uuidv4options-buffer-offset) | Create a version 4 (random) UUID | |
58| [`uuid.v5()`](#uuidv5name-namespace-buffer-offset) | Create a version 5 (namespace w/ SHA-1) UUID | |
59| [`uuid.validate()`](#uuidvalidatestr) | Test a string to see if it is a valid UUID | New in `uuid@8.3` |
60| [`uuid.version()`](#uuidversionstr) | Detect RFC version of a UUID | New in `uuid@8.3` |
61
62## API
63
64### uuid.NIL
65
66The nil UUID string (all zeros).
67
68Example:
69
70```javascript
71import { NIL as NIL_UUID } from 'uuid';
72
73NIL_UUID; // ⇨ '00000000-0000-0000-0000-000000000000'
74```
75
76### uuid.parse(str)
77
78Convert UUID string to array of bytes
79
80| | |
81| --------- | ---------------------------------------- |
82| `str` | A valid UUID `String` |
83| _returns_ | `Uint8Array[16]` |
84| _throws_ | `TypeError` if `str` is not a valid UUID |
85
86Example:
87
88```javascript
89import { parse as uuidParse } from 'uuid';
90
91uuidParse('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨
92 // Uint8Array(16) [
93 // 110, 192, 189, 127, 17,
94 // 192, 67, 218, 151, 94,
95 // 42, 138, 217, 235, 174,
96 // 11
97 // ]
98```
99
100### uuid.stringify(arr[, offset])
101
102Convert array of bytes to UUID string
103
104| | |
105| -------------- | --------------------------------------------------------------------------- |
106| `arr` | `Array`-like collection of 16 values (starting from `offset`) between 0-255 |
107| [`offset` = 0] | `Number` Starting index in the Array |
108| _returns_ | `String` |
109| _throws_ | `TypeError` if a valid UUID string cannot be generated |
110
111Example:
112
113```javascript
114import { stringify as uuidStringify } from 'uuid';
115
116const uuidBytes = [110, 192, 189, 127, 17, 192, 67, 218, 151, 94, 42, 138, 217, 235, 174, 11];
117
118uuidStringify(uuidBytes); // ⇨ '6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'
119```
120
121### uuid.v1([options[, buffer[, offset]]])
122
123Create an RFC version 1 (timestamp) UUID
124
125| | |
126| --- | --- |
127| [`options`] | `Object` with one or more of the following properties: |
128| [`options.node` ] | RFC "node" field as an `Array[6]` of byte values (per 4.1.6) |
129| [`options.clockseq`] | RFC "clock sequence" as a `Number` between 0 - 0x3fff |
130| [`options.msecs`] | RFC "timestamp" field (`Number` of milliseconds, unix epoch) |
131| [`options.nsecs`] | RFC "timestamp" field (`Number` of nanseconds to add to `msecs`, should be 0-10,000) |
132| [`options.random`] | `Array` of 16 random bytes (0-255) |
133| [`options.rng`] | Alternative to `options.random`, a `Function` that returns an `Array` of 16 random bytes (0-255) |
134| [`buffer`] | `Array \| Buffer` If specified, uuid will be written here in byte-form, starting at `offset` |
135| [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
136| _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
137| _throws_ | `Error` if more than 10M UUIDs/sec are requested |
138
139Note: 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.
140
141Note: `options.random` and `options.rng` are only meaningful on the very first call to `v1()`, where they may be passed to initialize the internal `node` and `clockseq` fields.
142
143Example:
144
145```javascript
146import { v1 as uuidv1 } from 'uuid';
147
148uuidv1(); // ⇨ '2c5ea4c0-4067-11e9-8bad-9b1deb4d3b7d'
149```
150
151Example using `options`:
152
153```javascript
154import { v1 as uuidv1 } from 'uuid';
155
156const v1options = {
157 node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
158 clockseq: 0x1234,
159 msecs: new Date('2011-11-01').getTime(),
160 nsecs: 5678,
161};
162uuidv1(v1options); // ⇨ '710b962e-041c-11e1-9234-0123456789ab'
163```
164
165### uuid.v3(name, namespace[, buffer[, offset]])
166
167Create an RFC version 3 (namespace w/ MD5) UUID
168
169API is identical to `v5()`, but uses "v3" instead.
170
171&#x26a0;&#xfe0f; Note: Per the RFC, "_If backward compatibility is not an issue, SHA-1 [Version 5] is preferred_."
172
173### uuid.v4([options[, buffer[, offset]]])
174
175Create an RFC version 4 (random) UUID
176
177| | |
178| --- | --- |
179| [`options`] | `Object` with one or more of the following properties: |
180| [`options.random`] | `Array` of 16 random bytes (0-255) |
181| [`options.rng`] | Alternative to `options.random`, a `Function` that returns an `Array` of 16 random bytes (0-255) |
182| [`buffer`] | `Array \| Buffer` If specified, uuid will be written here in byte-form, starting at `offset` |
183| [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
184| _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
185
186Example:
187
188```javascript
189import { v4 as uuidv4 } from 'uuid';
190
191uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
192```
193
194Example using predefined `random` values:
195
196```javascript
197import { v4 as uuidv4 } from 'uuid';
198
199const v4options = {
200 random: [
201 0x10,
202 0x91,
203 0x56,
204 0xbe,
205 0xc4,
206 0xfb,
207 0xc1,
208 0xea,
209 0x71,
210 0xb4,
211 0xef,
212 0xe1,
213 0x67,
214 0x1c,
215 0x58,
216 0x36,
217 ],
218};
219uuidv4(v4options); // ⇨ '109156be-c4fb-41ea-b1b4-efe1671c5836'
220```
221
222### uuid.v5(name, namespace[, buffer[, offset]])
223
224Createa an RFC version 5 (namespace w/ SHA-1) UUID
225
226| | |
227| --- | --- |
228| `name` | `String \| Array` |
229| `namespace` | `String \| Array[16]` Namespace UUID |
230| [`buffer`] | `Array \| Buffer` If specified, uuid will be written here in byte-form, starting at `offset` |
231| [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
232| _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
233
234Note: The RFC `DNS` and `URL` namespaces are available as `v5.DNS` and `v5.URL`.
235
236Example with custom namespace:
237
238```javascript
239import { v5 as uuidv5 } from 'uuid';
240
241// Define a custom namespace. Readers, create your own using something like
242// https://www.uuidgenerator.net/
243const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
244
245uuidv5('Hello, World!', MY_NAMESPACE); // ⇨ '630eb68f-e0fa-5ecc-887a-7c7a62614681'
246```
247
248Example with RFC `URL` namespace:
249
250```javascript
251import { v5 as uuidv5 } from 'uuid';
252
253uuidv5('https://www.w3.org/', uuidv5.URL); // ⇨ 'c106a26a-21bb-5538-8bf2-57095d1976c1'
254```
255
256### uuid.validate(str)
257
258Test a string to see if it is a valid UUID
259
260| | |
261| --------- | --------------------------------------------------- |
262| `str` | `String` to validate |
263| _returns_ | `true` if string is a valid UUID, `false` otherwise |
264
265Example:
266
267```javascript
268import { validate as uuidValidate } from 'uuid';
269
270uuidValidate('not a UUID'); // ⇨ false
271uuidValidate('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨ true
272```
273
274### uuid.version(str)
275
276Detect RFC version of a UUID
277
278| | |
279| --------- | ---------------------------------------- |
280| `str` | A valid UUID `String` |
281| _returns_ | `Number` The RFC version of the UUID |
282| _throws_ | `TypeError` if `str` is not a valid UUID |
283
284Example:
285
286```javascript
287import { version as uuidVersion } from 'uuid';
288
289uuidVersion('45637ec4-c85f-11ea-87d0-0242ac130003'); // ⇨ 1
290uuidVersion('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨ 4
291```
292
293## Command Line
294
295UUIDs can be generated from the command line using `uuid`.
296
297```shell
298$ uuid
299ddeb27fb-d9a0-4624-be4d-4615062daed4
300```
301
302The default is to generate version 4 UUIDS, however the other versions are supported. Type `uuid --help` for details:
303
304```shell
305$ uuid --help
306
307Usage:
308 uuid
309 uuid v1
310 uuid v3 <name> <namespace uuid>
311 uuid v4
312 uuid v5 <name> <namespace uuid>
313 uuid --help
314
315Note: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs
316defined by RFC4122
317```
318
319## ECMAScript Modules
320
321This 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).
322
323```javascript
324import { v4 as uuidv4 } from 'uuid';
325uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
326```
327
328To run the examples you must first create a dist build of this library in the module root:
329
330```shell
331npm run build
332```
333
334## CDN Builds
335
336### ECMAScript Modules
337
338To 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/):
339
340```html
341<script type="module">
342 import { v4 as uuidv4 } from 'https://jspm.dev/uuid';
343 console.log(uuidv4()); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
344</script>
345```
346
347### UMD
348
349To 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:
350
351**Using [UNPKG](https://unpkg.com/uuid@latest/dist/umd/)**:
352
353```html
354<script src="https://unpkg.com/uuid@latest/dist/umd/uuidv4.min.js"></script>
355```
356
357**Using [jsDelivr](https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/)**:
358
359```html
360<script src="https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/uuidv4.min.js"></script>
361```
362
363**Using [cdnjs](https://cdnjs.com/libraries/uuid)**:
364
365```html
366<script src="https://cdnjs.cloudflare.com/ajax/libs/uuid/8.1.0/uuidv4.min.js"></script>
367```
368
369These CDNs all provide the same [`uuidv4()`](#uuidv4options-buffer-offset) method:
370
371```html
372<script>
373 uuidv4(); // ⇨ '55af1e37-0734-46d8-b070-a1e42e4fc392'
374</script>
375```
376
377Methods for the other algorithms ([`uuidv1()`](#uuidv1options-buffer-offset), [`uuidv3()`](#uuidv3name-namespace-buffer-offset) and [`uuidv5()`](#uuidv5name-namespace-buffer-offset)) are available from the files `uuidv1.min.js`, `uuidv3.min.js` and `uuidv5.min.js` respectively.
378
379## "getRandomValues() not supported"
380
381This 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:
382
383### React Native
384
3851. Install [`react-native-get-random-values`](https://github.com/LinusU/react-native-get-random-values#readme)
3861. Import it before `uuid`:
387
388```javascript
389import 'react-native-get-random-values';
390import { v4 as uuidv4 } from 'uuid';
391```
392
393### Web Workers / Service Workers (Edge <= 18)
394
395[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).
396
397## Upgrading From `uuid@7.x`
398
399### Only Named Exports Supported When Using with Node.js ESM
400
401`uuid@7.x` 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.
402
403Instead of doing:
404
405```javascript
406import uuid from 'uuid';
407uuid.v4();
408```
409
410you will now have to use the named exports:
411
412```javascript
413import { v4 as uuidv4 } from 'uuid';
414uuidv4();
415```
416
417### Deep Requires No Longer Supported
418
419Deep requires like `require('uuid/v4')` [which have been deprecated in `uuid@7.x`](#deep-requires-now-deprecated) are no longer supported.
420
421## Upgrading From `uuid@3.x`
422
423"_Wait... what happened to `uuid@4.x` - `uuid@6.x`?!?_"
424
425In order to avoid confusion with RFC [version 4](#uuidv4options-buffer-offset) and [version 5](#uuidv5name-namespace-buffer-offset) UUIDs, and a possible [version 6](http://gh.peabody.io/uuidv6/), releases 4 thru 6 of this module have been skipped.
426
427### Deep Requires Now Deprecated
428
429`uuid@3.x` encouraged the use of deep requires to minimize the bundle size of browser builds:
430
431```javascript
432const uuidv4 = require('uuid/v4'); // <== NOW DEPRECATED!
433uuidv4();
434```
435
436As of `uuid@7.x` 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:
437
438```javascript
439import { v4 as uuidv4 } from 'uuid';
440uuidv4();
441```
442
443... or for CommonJS:
444
445```javascript
446const { v4: uuidv4 } = require('uuid');
447uuidv4();
448```
449
450### Default Export Removed
451
452`uuid@3.x` was exporting the Version 4 UUID method as a default export:
453
454```javascript
455const uuid = require('uuid'); // <== REMOVED!
456```
457
458This usage pattern was already discouraged in `uuid@3.x` and has been removed in `uuid@7.x`.
459
460----
461Markdown 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