Last modified: Jun 23, 2026

Fix Cannot find module 'joi' Error

You are coding in Node.js and suddenly see the error "Cannot find module 'joi'". This is a common issue for beginners. It usually happens when the joi package is missing or not installed correctly.

This article will show you how to fix it. We will use short steps and clear examples. By the end, you will understand the cause and the solution.

What does the error mean?

The error means Node.js cannot locate the joi module in your project. joi is a popular library for data validation. It helps you check if data is correct, like email formats or password strength.

When you run your code, Node.js looks for joi in the node_modules folder. If it is not there, you get this error. This can happen for three main reasons:

  • You forgot to install joi.
  • The installation failed.
  • You are in the wrong directory.

Step 1: Check if joi is installed

First, check if joi is in your project. Open your terminal and run this command:


npm list joi

If you see joi@version, it is installed. If you see (empty) or an error, it is missing.

You can also check the package.json file. Look for "joi" in the dependencies section.

Step 2: Install joi correctly

If joi is missing, install it using npm. Run this command in your project folder:


npm install joi

This will download the package and add it to node_modules. It also updates package.json and package-lock.json.

If you are using Yarn, use:


yarn add joi

After installation, run npm list joi again to confirm it is there.

Step 3: Check your import statement

Make sure you are importing joi correctly. For older versions (before version 17), use require:


const Joi = require('joi');

For newer versions (17 and above), you might need to use import if your project uses ES modules. Add "type": "module" to your package.json and write:


import Joi from 'joi';

If you use CommonJS, stick with require. Mixing them can cause errors.

Step 4: Verify the Node.js version

Some joi versions require a specific Node.js version. Check your Node version with:


node --version

If you have an old Node.js (like 10 or 12), update it. joi version 17+ works best with Node.js 12 or later. Use a version manager like nvm to update easily.

Step 5: Clear the npm cache

A corrupted cache can cause install issues. Clear it with:


npm cache clean --force

Then reinstall joi:


npm install joi

This often fixes strange errors.

Step 6: Delete node_modules and reinstall

If nothing works, delete the node_modules folder and package-lock.json. Then reinstall all dependencies:


rm -rf node_modules package-lock.json
npm install

This gives you a fresh start. It solves many dependency problems.

Example code with joi

Here is a simple example to test if joi works. Create a file test.js:


// Import joi using require (CommonJS)
const Joi = require('joi');

// Define a schema
const schema = Joi.object({
    username: Joi.string().alphanum().min(3).max(30).required(),
    email: Joi.string().email().required()
});

// Test data
const data = {
    username: 'john_doe',
    email: 'john@example.com'
};

// Validate data
const result = schema.validate(data);

if (result.error) {
    console.log('Validation error:', result.error.details);
} else {
    console.log('Validation passed!');
}

Run the code:


node test.js

Expected output:


Validation passed!

If you get "Cannot find module 'joi'", revisit steps 2 and 3.

Common mistakes to avoid

Beginners often make these mistakes:

  • Installing joi globally instead of locally. Use npm install joi without -g.
  • Typing the name wrong. It is joi, not jio or joi with capital letters.
  • Running code from a different folder. Always run node from the project root.

If you need more help with similar issues, check our guide on Fix Node.js Error: Cannot find module for general troubleshooting.

Conclusion

The error "Cannot find module 'joi'" is easy to fix. Install joi with npm install joi. Check your import statement and Node.js version. If needed, clear the cache or reinstall dependencies.

Always test with a simple script like the one above. This confirms the module works. Now you can use joi for powerful data validation in your Node.js projects.