Reputation: 2819
I try to make a uuid
(v 3.0.1) package work in Node/Typescript app, but I'm not sure what should I import and how to use it.
This is index.d.ts
(from @types/uuid
v 2.0.29):
declare namespace uuid {
interface V1Options {
node?: number[];
clockseq?: number;
msecs?: number | Date;
nsecs?: number;
}
type V4Options = { random: number[] } | { rng: () => number[]; }
interface UuidStatic {
(options?: V4Options): string;
(options: V4Options | null, buffer: number[], offset?: number): number[];
(options: V4Options | null, buffer: Buffer, offset?: number): Buffer;
v1(options?: V1Options): string;
v1(options: V1Options | null, buffer: number[], offset?: number): number[];
v1(options: V1Options | null, buffer: Buffer, offset?: number): Buffer;
v4: UuidStatic;
parse(id: string): number[];
parse(id: string, buffer: number[], offset?: number): number[];
parse(id: string, buffer: Buffer, offset?: number): Buffer;
unparse(buffer: number[] | Buffer, offset?: number): string;
}
}
declare const uuid: uuid.UuidStatic
export = uuid
I cant find exported class here.
For example index.d.ts
from angular2-uuid
looks like that:
export declare class UUID {
constructor();
static UUID(): string;
private static pad4(num);
private static random4();
}
And it's quite obvious to use:
let id = UUID.UUID();
So. How to use (import and call) uuid
?
Upvotes: 104
Views: 164393
Reputation: 97
Even faster is to just use the builtin browser function:
myuuid = self.crypto.randomUUID()
See Mozilla's Crypto: randomUUID() method
Upvotes: 2
Reputation: 23
import { v4 as uuid } from 'uuid';
if use TypeScript should install definitions:
npm i --save-dev @types/uuid
Upvotes: 2
Reputation: 149
firstly install these two packages
npm i --save-dev @types/uuid
npm install uuid
Then by using uuid package, you can easily do your job.
import { v4 as uuidv4 } from 'uuid';
const myuuid = uuidv4();
console.log('Your UUID is: 'myuuid);
Upvotes: 2
Reputation: 1679
Quoting Geshan Manandhar from his blogpost 3 efficient ways to generate UUID in Node.js you can use a built in Nodejs package:
Node.js UUID with Crypto module
The crypto module was added from Node.js 14.17.0. It provides cryptographic functionally for multiple methods and algorithms like OpenSSL’s hash, HMAC, cipher. It also provides a method called randomUUID to generate UUID in Node.js without instaling any new NPM module.
The method takes an options object that can have a
disableEntropyCache
boolean value that defaults to values. When it is set to true it doesn’t use the cache on the UUID generation. Below is a code example of Cryto module’srandomUUID
function:
const crypto = require('crypto');
console.log(crypto.randomUUID());
You can also use it this way:
import { randomUUID } from 'crypto';
console.log(randomUUID());
Upvotes: 4
Reputation:
I used uuid for my project in the following order
Must be installed
npm i --save-dev @types/uuid
npm install uuid
Using the uuid package
import { v4 as uuidv4 } from 'uuid';
const myuuid = uuidv4();
console.log('Your UUID is: 'myuuid);
Upvotes: 11
Reputation: 4814
Yes, here is code from my project:
import { v4 as uuid } from 'uuid';
const id: string = uuid();
Note: to install definitions it is required to run
npm install --save-dev @types/uuid
Upvotes: 210
Reputation: 6338
This also works for me:
import uuidv4 from 'uuid/v4'
this.projectId = uuidv4()
Upvotes: 6
Reputation: 42656
Per the tests:
import uuid = require('uuid');
let uuidv4: string = uuid.v4();
Upvotes: 13