Last modified: Jun 24, 2026
Fix Yup Module Not Found Error
Running a Node.js application and seeing the error Cannot find module 'yup' can be frustrating. This error stops your app from starting and often appears when using form validation or schema libraries. But don't worry. This guide will walk you through all the common causes and solutions. By the end, you will have your app running smoothly again.
What Does This Error Mean?
The error means Node.js cannot locate the yup package in your project's node_modules folder. This usually happens because the package is missing, not installed correctly, or your project is looking in the wrong place. The yup library is popular for schema validation in JavaScript and TypeScript applications. When it is missing, any import or require statement for it will fail.
Common Causes of the Error
Several reasons can trigger this error. The most common ones include:
- The
yuppackage is not installed in your project. - You installed it in the wrong directory or environment.
- Your
package.jsonfile is missing the dependency entry. - There is a version conflict with other packages.
- You are using a monorepo or workspace setup with incorrect symlinks.
Understanding the root cause helps you apply the right fix quickly. Let's explore each solution step by step.
Step 1: Install or Reinstall the Yup Package
The first and simplest fix is to install the yup package. If you have never installed it, run the following command in your project root:
npm install yup
If you use Yarn, run:
yarn add yup
If the package was already installed but still causes errors, try reinstalling it. Remove the node_modules folder and package-lock.json file, then install again:
rm -rf node_modules package-lock.json
npm install
This ensures a clean installation without corrupted files. After this, test your app again.
Step 2: Check Your Import or Require Statement
Another common mistake is using the wrong syntax to import yup. In modern JavaScript (ES modules), you should use import. In older CommonJS projects, you use require. Mixing them can cause the module not to be found.
For ES modules, use:
import * as yup from 'yup';
For CommonJS, use:
const yup = require('yup');
Make sure your package.json has "type": "module" if you use ES modules. If not, Node.js defaults to CommonJS. Using the wrong syntax will throw the error.
Step 3: Verify Your Project Structure and Path
Sometimes the error occurs because you are running the script from the wrong directory. Node.js looks for modules in the node_modules folder of the current working directory. If you run your script from a subfolder, it may not find yup.
To check, run this command in your terminal:
node -e "console.log(require.resolve('yup'))"
If it returns a path, the module is found. If it throws an error, the module is missing. Ensure you are executing your script from the project root where package.json and node_modules live.
If you are using a monorepo with workspaces, you might need to install yup in the specific package that uses it. For example:
cd packages/my-app
npm install yup
Then run your script from that package directory.
Step 4: Check for Version Conflicts
Sometimes another package in your project depends on a different version of yup. This can cause Node.js to fail to resolve the module. Use npm ls yup to see all versions installed:
npm ls yup
If you see multiple versions, you may have a conflict. To fix this, remove the node_modules folder and reinstall everything. If the issue persists, check if one of your dependencies explicitly requires an older version. You can update yup to the latest version:
npm install yup@latest
This ensures compatibility with your other packages.
Step 5: Clear NPM Cache and Rebuild
Corrupted cache can also cause module resolution errors. Clear the npm cache and try again:
npm cache clean --force
npm install
After cleaning, your node_modules will be rebuilt fresh. This often resolves mysterious module errors.
Example: A Simple Yup Validation Script
Here is a complete example to test if yup works correctly. Create a file named test.js:
// test.js - Yup validation example
import * as yup from 'yup';
// Define a schema
const schema = yup.object().shape({
name: yup.string().required('Name is required'),
age: yup.number().positive().integer().required(),
});
// Test validation
async function validateUser(data) {
try {
const validData = await schema.validate(data, { abortEarly: false });
console.log('Validation passed:', validData);
} catch (err) {
console.error('Validation failed:', err.errors);
}
}
// Run the test
validateUser({ name: 'Alice', age: 30 });
validateUser({ name: '', age: -5 });
Run the script:
node test.js
Expected output:
Validation passed: { name: 'Alice', age: 30 }
Validation failed: [ 'Name is required', 'age must be a positive number' ]
If you see this output, yup is working correctly. If you get the original error, revisit the steps above.
How to Prevent This Error in the Future
To avoid the "Cannot find module 'yup'" error in future projects, follow these best practices:
- Always run
npm installafter cloning a repository. - Use a
package-lock.jsonfile to lock dependency versions. - Check your
package.jsonfor missing entries before sharing code. - Use version control like Git to track changes in dependencies.
- If you encounter similar issues with other modules, read our guide on how to Fix Node.js Error: Cannot find module for general solutions.
Consistent practices keep your development environment stable and reduce debugging time.
Conclusion
The Cannot find module 'yup' error is usually easy to fix. Start by installing or reinstalling the package. Check your import syntax and project path. If the problem persists, look for version conflicts or cache issues. With the steps in this guide, you can resolve the error quickly and get back to building your application. Remember, keeping your dependencies clean and your project structure organized prevents many common Node.js errors.