When you are running your application, you could be irritated by the error message cannot find module ‘ajv/dist/compile/codegen’ if you operate with React Application or Another JSON Schema Validator. Let’s now explain what this error implies, why it happens, and how can you fix it on your own.
Why Does This Error Occur?
Table of Contents
When your code tries to import a module or file that does not exist or cannot be found in the supplied path, the error message “Cannot find module” typically appears.
How to Fix the Error
Here are some simple steps you can follow to resolve this error.
Delete node_modules and package-lock.json
Reinstalling dependencies and removing these folders/files might sometimes fix problems.
rm -rf node_modules
rm package-lock.json
Clear npm Cache
To make sure that no corrupted files are being used during installation, clear the npm cache.
npm cache clean --force
Reinstall Dependencies
Reinstall your project’s dependencies.
npm install
Ensure Compatibility
Verify the compatibility of your ajv and ajv-keywords versions. This can be manually verified within your package.Install compatible versions or use json.
For example, if your ajv version is 7. x, ensure the ajv-keywords version is also compatible with it.
npm install ajv@latest ajv-keywords@latest
Run the Application
Reinstall the dependencies, then try launching your application once more.
npm start
Update your react scripts to the most recent version if the solutions above don’t work; it may have patches for dependency problems like these.
|
Once done, rerun the application:
npm start
Final Thought
Cannot find module ‘ajv/dist/compile/codegen’ this is annoying, but it’s normally pretty easy to fix. We have provided code snippets at each stage to guide you through the process seamlessly. If you’re encountering difficulties, feel free to reach out to the developer community for assistance or refer to the documentation of the relevant packages.
FAQ’s
What does this mean: “Cannot find module ‘ajv/dist/compile/codegen’?
This indicates that your application is trying to import or reference a missing, incompatible file in your dependencies, especially if you made use of Ajv.
Why am I getting this error in my React application?
You get this error because Ajv or associated modules are missing, is installed, or clashing with other packages in your React app.
Why Might This Issue Be Resolved By Deleting The Node_Modules Folder?
Deleting the node_modules folder and the package-lock.json deletes all installed dependencies and locks. Reinstalling them results in having a fresh and correct version of your dependencies, which can remove the conflict or missing files.
Why should I clean up my npm cache?
The cleaning of the npm cache removes files that might have been corrupted or outdated, hence causing malfunctions when reinstalling their dependencies. This ensures clean installation.
What do I do when reinstalling dependencies does not work?
If reinstalling dependencies does not work, try to update one of the related libraries, like react-scripts, or the versions of ajv and ajv-keywords to match. Just ensure you are matching versions appropriately for your configuration.