Attempted import error: 'uuid' does not contain a default export in JS

avatar

Borislav Hadzhiev

Last updated: Feb 22, 2022

banner

Photo from Unsplash

Attempted import error: 'uuid' does not contain a default export in JS #

The "uuid does not contain a default export" error occurs because the uuid package does not have a default export. To solve the error, use a named import to import one of the module's functions, e.g. import {v4 as uuidv4} from 'uuid'.

uuid does not contain default export

Here is an example of how the error occurs.

index.js
// ⛔️ Error: 'uuid' does not contain a default export // 👇️ trying to use a default import import uuid from 'uuid';

To solve the error, use a named import to import one of the functions the uuid module exports.

index.js
// ✅ Works // 👇️ using a named import import {v4 as uuidv4} from 'uuid'; console.log(uuidv4()); // "88d33c46-8af0-40df-9e38-b34c30cb20a5"

The reason this error occurs is that the uuid module only has named exports.

The module's exports look something like:

index.js
// 👇️ named export export function v4() { return 'random string'; } // 👇️ named export export function v4() { return 'random string'; }

The module doesn't have a default export, which would look like:

index.js
export default { v4() { return 'random string' }, v5() { return 'random string' } }

Since the uuid module only uses named exports, you have to only used named imports when importing functions from it.

index.js
// 👇️ using named imports import {v1 as uuidv1, v4 as uuidv4} from 'uuid'; console.log(uuidv1()); // 👉️ af93b370-93bc-11ec-9ab5-4119d0fecea0 console.log(uuidv4()); // 👉️ 45d2c930-a76e-4755-bc4b-c6179577247d

Conclusion #

The "uuid does not contain a default export" error occurs because the uuid package does not have a default export. To solve the error, use a named import to import one of the module's functions, e.g. import {v4 as uuidv4} from 'uuid'.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.