TypeError: uuid.v4 is not a function

janac
May 17, 2021

For other Typescript newbies like me that want to understand why this is happening — it’s because the `v4()` function is exported in the ‘uuid’ source code, which means we need to `import` it to gain access to it. So we specifically need to import the `v4` function, and then can use it as we wish:

import { v4 } from ‘uuid’;v4(); // Produces a UUID

However, if you just see `v4()` in the code, it wouldn’t be immediately obvious that this function returns a UUID. So we can add an alias to enhance code readability:

import { v4 as uuid } from ‘uuid’;uuid(); // Produces a UUID

See here for the official docs.

--

--

janac
janac

Written by janac

Most of my writing is about software. I enjoy summarizing and analyzing books and self-help videos. I am senior software consultant at LazerTechnologies.com.

No responses yet