Cannot find module 'uuid' error

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Cannot find module 'uuid' error #

To solve the error "Cannot find module 'uuid'", make sure to install the uuid package by opening your terminal in your project's root directory and running the following command: npm i uuid. If you use TypeScript, install the typings by running npm i -D @types/uuid.

index.js
// ⛔️ Cannot find module 'uuid' // Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'uuid' // imported from bobbyhadz-js/index.js import {v4 as uuidv4} from 'uuid'; console.log(uuidv4()); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'

Open your terminal in your project's root directory (where your package.json file is located) and run the following commands:

shell
npm install uuid # 👇️ only if you use TypeScript npm install --save-dev @types/uuid
This will add the uuid package to the dependencies of your project.

Now you should be able to import and use the package like so:

index.js
import {v4 as uuidv4} from 'uuid'; console.log(uuidv4()); // 👉️ e00013764-617d-49a8-8aa0-f1234585jak

If the error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.

shell
# 👇️ delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # 👇️ clean npm cache npm cache clean --force npm install
Make sure to restart your IDE and dev server if the error persists. VSCode often glitches and needs a reboot.

If you still get the "Cannot find module 'uuid'" error, open your package.json file and make sure it contains the uuid package in the dependencies object.

package.json
{ // ... rest "dependencies": { "uuid": "^8.3.2", }, "devDependencies": { // 👇️ only if you use TypeScript "@types/uuid": "^8.3.4", } }

You can try to manually add the line and re-run npm install.

shell
npm install

Or install the latest version of the package:

shell
npm install uuid@latest

Conclusion #

To solve the error "Cannot find module 'uuid'", make sure to install the uuid package by opening your terminal in your project's root directory and running the following command: npm i uuid. If you use TypeScript, install the typings by running npm i -D @types/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.