Last modified: Jun 23, 2026
Fix Cannot find module 'uuid' Error
Getting the error Cannot find module 'uuid' in Node.js is common for beginners. It usually means the uuid package is missing from your project. This article shows you how to fix it fast and avoid it again.
We will cover installation, common mistakes, and version issues. You will also learn how to check your setup. Let's start with the simplest solution.
What Causes the 'uuid' Module Error?
This error happens when Node.js cannot locate the uuid package in your project's node_modules folder. The most common reasons are:
- You forgot to install the package.
- You are running the script from the wrong directory.
- Your
package.jsonis missing the dependency. - You have a version mismatch.
This error can stop your application from running. It is important to fix it correctly.
How to Install the uuid Package
The first step is to install uuid using npm. Open your terminal and navigate to your project folder. Then run this command:
npm install uuid
This command downloads the latest version of uuid and adds it to node_modules. It also updates your package.json file. After installation, your error should disappear.
If you are using yarn, run this instead:
yarn add uuid
Both commands work the same way. Choose the one you prefer.
Check Your Project Structure
Sometimes you are in the wrong folder. Make sure your terminal is inside the project root. The package.json file must be present in that directory. Run this command to verify:
ls package.json
If you see the file, you are in the right place. If not, change to the correct directory using cd.
Common Mistake: Missing package.json
A missing package.json file can also cause this error. If you never ran npm init, your project has no dependency list. Create one with:
npm init -y
This creates a default package.json. Then install uuid again. This ensures the package is tracked properly.
Version Issues with uuid
The uuid package has different versions. Version 9 and above use ES modules by default. If your project uses CommonJS (require), you might face issues. Check your package.json for the version:
npm list uuid
If you see version 9 or higher, and you use require, install version 8 instead:
npm install uuid@8
This older version works with CommonJS. Version 9 works with import statements.
Example Code Using uuid
Here is a simple example that generates a random UUID. Make sure you have installed the package first.
// Import the uuid package
const { v4: uuidv4 } = require('uuid');
// Generate a new UUID
const myId = uuidv4();
// Print the UUID to the console
console.log("Your new UUID is:", myId);
Save this code in a file, for example app.js. Then run it:
node app.js
You should see output like this:
Your new UUID is: 9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d
If you get the error again, double-check your installation.
Using uuid with ES Modules
If your project uses ES modules (type: "module" in package.json), use import instead of require:
// Import uuid with ES modules
import { v4 as uuidv4 } from 'uuid';
// Generate a UUID
const myId = uuidv4();
console.log("Your UUID:", myId);
This works with version 9 and above. Make sure your package.json has "type": "module".
How to Avoid This Error in the Future
Always run npm install after cloning a project. This installs all dependencies listed in package.json. Never delete the node_modules folder manually unless you plan to reinstall.
Use a .gitignore file to exclude node_modules from version control. This keeps your repo clean and avoids confusion.
If you encounter similar errors with other packages, check out this guide on how to Fix Node.js Error: Cannot find module for a general approach.
Check Node.js and npm Versions
Outdated tools can cause package issues. Verify your Node.js and npm versions:
node -v
npm -v
If you have an old version, update Node.js from the official website. This ensures compatibility with modern packages like uuid.
Clear npm Cache If Needed
A corrupted cache can lead to installation problems. Clear it with:
npm cache clean --force
Then reinstall uuid. This often fixes stubborn errors.
Conclusion
The Cannot find module 'uuid' error is easy to fix. Install the package with npm install uuid, check your working directory, and ensure your package.json is present. Pay attention to version compatibility between CommonJS and ES modules. By following these steps, you can resolve the issue quickly and get back to coding.
For more help with similar errors, refer to the Fix Node.js Error: Cannot find module article. It covers common pitfalls and solutions for missing modules in Node.js.