97

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?

tBlabs
  • 2,619
  • 3
  • 18
  • 22

6 Answers6

196

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
Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
Sergey Yarotskiy
  • 4,536
  • 2
  • 19
  • 27
13

Per the tests:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/354cec620daccfa0ad167ba046651fb5fef69e8a/types/uuid/uuid-tests.ts

import uuid = require('uuid');

let uuidv4: string = uuid.v4();
Joe
  • 41,484
  • 20
  • 104
  • 125
9
import * as uuid from "uuid";
const id: string = uuid.v4();
Steve Gula
  • 184
  • 2
  • 7
7

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);
Mild Fuzz
  • 29,463
  • 31
  • 100
  • 148
6

This also works for me:

import uuidv4 from 'uuid/v4'

this.projectId = uuidv4()
GaryO
  • 5,873
  • 1
  • 36
  • 61
1

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’s randomUUID function:

const crypto = require('crypto');
console.log(crypto.randomUUID());

You can also use it this way:

import { randomUUID } from 'crypto';
console.log(randomUUID());
Michał J. Gąsior
  • 1,457
  • 3
  • 21
  • 39