TypeError: uuid_1.v5 is not a function

Viewed 12478

Attempting to generate a UUID with the package uuid using these steps:

Installation

npm i -S uuid @types/uuid

Code

    import { v5 } from 'uuid';
    const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
    console.log(v5('Hello, World!', MY_NAMESPACE));

When I run that I get:

    TypeError: uuid_1.v5 is not a function
        at Object.<anonymous> (/home/ole/slice/test.ts:3:13)
        at Module._compile (internal/modules/cjs/loader.js:654:30)

Thoughts?

5 Answers

DefinitelyTyped is wrong: the uuid module does not export v5. Your import should be:

import v5 = require('uuid/v5');

Or if you have esModuleInterop enabled, you can use:

import v5 from 'uuid/v5';

I was also getting the similar problem.

Try the following and it will work.

//I use v4 so , but try .v5 if you want to
const uuid = require('uuid').v4

//now can call uuid
uuid()
//import uuid

import {v4 as uuid} from 'uuid'

//call uuid

id:uuid

In TypeScript Backend

Try the following and it should work.

    import { v4 as uuid } from 'uuid';

then call like:

id: uuid()

It worked for me.

If you're from the future (present) and seeing this page, please reinstall uuid. You've probably got some version < 3.3.3 installed. As of today, the current version is 8.x, which does export v5.

Related