guid/uuid in Typescript Node.js app

node.jsTypescriptUuidGuid

node.js Problem Overview


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?

node.js Solutions


Solution 1 - node.js

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

Solution 2 - node.js

Solution 3 - node.js

import * as uuid from "uuid";
const id: string = uuid.v4();

Solution 4 - node.js

This also works for me:

import uuidv4 from 'uuid/v4'

this.projectId = uuidv4()

Solution 5 - node.js

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';
let myuuid = uuidv4();
console.log('Your UUID is: 'myuuid);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontBlabsView Question on Stackoverflow
Solution 1 - node.jsSergey YarotskiyView Answer on Stackoverflow
Solution 2 - node.jsJoeView Answer on Stackoverflow
Solution 3 - node.jsSteve GulaView Answer on Stackoverflow
Solution 4 - node.jsGaryOView Answer on Stackoverflow
Solution 5 - node.jsuser15630604View Answer on Stackoverflow