Last modified: Jun 24, 2026
Fix Cannot find module 'zod' Error
Encountering the error "Cannot find module 'zod'" in Node.js can be frustrating, especially when you are just starting out. This error usually means that the Zod library is not installed or cannot be located by your Node.js project. Zod is a popular TypeScript-first schema validation library, but it works seamlessly with JavaScript too. In this article, we will walk through the most common causes and solutions to get your code running smoothly again.
Before diving into fixes, it's important to understand that Node.js looks for modules in the node_modules folder. If Zod is missing from there, or if your project's package.json does not list it, you will see this error. The good news is that the solution is usually straightforward. Let's explore each step in detail.
1. Install Zod Using npm or yarn
The most common reason for this error is that Zod is simply not installed. To fix it, you need to run the appropriate package manager command in your project's root directory. Open your terminal and navigate to the folder containing your package.json file.
Use one of the following commands based on your package manager:
# Using npm
npm install zod
# Using yarn
yarn add zod
# Using pnpm
pnpm add zod
After installation, check your node_modules folder to confirm Zod is present. You should see a zod directory inside it. This step usually resolves the error for most beginners. If the error persists, move on to the next section.
2. Check Your package.json and package-lock.json
Sometimes, the installation might have failed silently, or the package.json file may not have been updated. Verify that Zod is listed under "dependencies" or "devDependencies" in your package.json. If it's missing, re-run the install command from step 1.
Also, ensure that your package-lock.json (or yarn.lock) is up to date. You can delete the lock file and node_modules folder, then run npm install again to regenerate everything from scratch. This is a reliable way to fix corrupted installations.
# Clean reinstall approach
rm -rf node_modules package-lock.json
npm install
After running this, your project should have a fresh copy of Zod and all its dependencies. This method often resolves issues related to version mismatches or partial downloads.
3. Verify Your Import Statement
Another common mistake is using the wrong import syntax. Zod can be imported in two ways, depending on whether you are using CommonJS or ES modules. Make sure your import matches your project's module system.
If you are using CommonJS (default in Node.js), use require:
// CommonJS syntax (Node.js default)
const { z } = require('zod');
If you are using ES modules (by setting "type": "module" in your package.json), use import:
// ES module syntax
import { z } from 'zod';
Using the wrong syntax will cause Node.js to fail to find the module, even if it is installed. Always double-check your package.json to see which module system is active. If you are unsure, check for the "type": "module" line.
4. Check for Global vs Local Installation
Node.js modules are typically installed locally to your project. If you installed Zod globally using npm install -g zod, your project may not be able to find it. Global installations are meant for CLI tools, not for library dependencies.
To fix this, uninstall the global version and install it locally:
# Remove global installation (if present)
npm uninstall -g zod
# Install locally in your project
npm install zod
Then, run your script again. Your project should now be able to locate the module in the local node_modules folder. This is a common pitfall for beginners who are new to Node.js package management.
5. Ensure Your Node.js Version is Compatible
Zod requires a modern Node.js version (usually Node.js 14 or higher). If you are using an older version, you might encounter compatibility issues. Check your Node.js version with node --version in the terminal.
If your version is too old, update Node.js to the latest LTS release. You can download it from the official website or use a version manager like nvm. After updating, run npm install again to ensure all packages work correctly.
# Check your Node.js version
node --version
# Example output: v16.20.0 (if this is lower than v14, update)
Updating Node.js often resolves hidden compatibility issues that prevent modules from being loaded properly. It is a good practice to keep your runtime environment up to date.
6. Use a Module Resolution Path
If you are running your script from a different directory than the project root, Node.js may not find node_modules. Always run your script from the root folder of your project, or use the --experimental-specifier-resolution flag for ES modules.
For CommonJS, ensure you are in the correct directory:
# Run from project root
cd /path/to/your/project
node index.js
If you have a complex folder structure, you can also use the NODE_PATH environment variable to specify where to look for modules. However, this is less common and usually not needed for simple projects. Stick to running from the root directory for best results.
7. Example: Using Zod in a Simple Script
Let’s put everything together with a working example. Create a file called validate.js and add the following code:
// Import Zod using CommonJS (default)
const { z } = require('zod');
// Define a simple schema
const userSchema = z.object({
name: z.string().min(2),
age: z.number().positive(),
});
// Validate some data
const data = { name: "Alice", age: 25 };
const result = userSchema.safeParse(data);
if (result.success) {
console.log("Validation passed:", result.data);
} else {
console.log("Validation failed:", result.error.errors);
}
Run this script from your project root:
node validate.js
Expected output:
Validation passed: { name: 'Alice', age: 25 }
If you see this output, your installation is working correctly. If you still get the "Cannot find module 'zod'" error, double-check the steps above. The issue is almost always related to installation or import syntax.
Conclusion
Fixing the "Cannot find module 'zod'" error is usually a matter of installing the package correctly, using the right import syntax, and ensuring your project environment is set up properly. By following the steps in this guide—installing Zod locally, checking your package.json, verifying your import statement, and updating Node.js—you can resolve this error quickly. Remember to always run your scripts from the project root and avoid global installations for library dependencies. If you encounter similar issues with other modules, check out our guide on Fix Node.js Error: Cannot find module for more general troubleshooting tips. With these practices, you will be able to use Zod and other npm packages without hassle.