在TypeScript Node.js应用中使用GUID/UUID

98

我尝试在Node / Typescript应用程序中使用uuid(v 3.0.1)包,但我不确定应该导入什么以及如何使用它。

这是index.d.ts(来自@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

我找不到这里导出的类。

例如,来自angular2-uuidindex.d.ts看起来像是这样:

export declare class UUID {
    constructor();
    static UUID(): string;
    private static pad4(num);
    private static random4();
}

而且使用起来非常明显:

let id = UUID.UUID();

那么,如何使用(导入和调用)uuid


您的类型版本与实际包不匹配,请确保它们相同。 - dwjohnston
8个回答

197

好的,这是我的项目中的代码:

import { v4 as uuid } from 'uuid';
const id: string = uuid();

注意:安装定义需要运行

npm install --save-dev @types/uuid

7
或者只需导入 import { v4 } from 'uuid'; 就能解决我的问题。谢谢! - tBlabs
1
这个不行。错误:'v4'未被node_modules/uuid/index.js导出。 - Steve
刚刚在最新版本的uuid模块上重新测试了一下,一切都按预期工作。我建议你在SO上开始另一个问题。 - Sergey Yarotskiy

13

1
可以了!谢谢,但是... 有没有办法使用 import { } from "" TypeScript 风格? - tBlabs
这对TypeScript效果最佳。排名第一的答案抱怨了来自linter的任何警告。 - eVolve

9

我在我的项目中按照以下顺序使用了uuid

必须安装

npm i --save-dev @types/uuid
npm install uuid

使用uuid包

import { v4 as uuidv4 } from 'uuid';
const myuuid = uuidv4();
console.log('Your UUID is: 'myuuid);

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

6

这对我也有效:

import uuidv4 from 'uuid/v4'

this.projectId = uuidv4()

2
引用Geshan Manandhar在他的博客文章中3 efficient ways to generate UUID in Node.js的话,你可以使用一个内置的Nodejs包:

带有Crypto模块的Node.js UUID

加密模块从Node.js 14.17.0开始添加。它为多种方法和算法提供了加密功能,例如OpenSSL哈希、HMAC、密码。它还提供了一个名为randomUUID的方法,在不安装任何新的NPM模块的情况下生成Node.js中的UUID。

该方法接受一个选项对象,该对象可以具有一个disableEntropyCache布尔值,默认值为values。当将其设置为true时,它不会在UUID生成上使用缓存。以下是Cryto模块的randomUUID函数的代码示例:

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

你也可以这样使用:

import { randomUUID } from 'crypto';
console.log(randomUUID());

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

如果使用TypeScript,应该安装定义文件。
npm i --save-dev @types/uuid

你的回答可以通过提供更多的支持性信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的回答是否正确。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - undefined

0
首先安装这两个软件包。
npm i --save-dev @types/uuid 
npm install uuid

然后通过使用uuid包,你可以轻松完成你的工作。
import { v4 as uuidv4 } from 'uuid';
const myuuid = uuidv4();
console.log('Your UUID is: 'myuuid);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接