106

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?

1
  • You have the wrong versions of types to the actual package. Make sure they match. Commented Aug 22, 2020 at 6:29

9 Answers 9

215

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
Sign up to request clarification or add additional context in comments.

4 Comments

or just import { v4 } from 'uuid'; That's solved my problem. Thanks!
This doesn't work. Error: 'v4' is not exported by node_modules/uuid/index.js
Just re-tested on the latest version of uuid module. Everything works as expected. I'll suggest you start another question on SO.
Another mention, that I had to restart my VSCode before the error goes away. No idea why but this is the correct solution.
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();

2 Comments

That's works! Thanks, but... Is there any way to use import { } from "" typescript style?
This works best for Typescript. Top answer was complaining about any warnings from linter
12

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);

Comments

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

Comments

6

This also works for me:

import uuidv4 from 'uuid/v4'

this.projectId = uuidv4()

Comments

4

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());

Comments

2

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);

Comments

2
import { v4 as uuid } from 'uuid';

if use TypeScript should install definitions:

npm i --save-dev @types/uuid

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
2

Even faster is to just use the builtin browser function:

      myuuid = self.crypto.randomUUID()

See Mozilla's Crypto: randomUUID() method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.