How to Fix Module Not Found Error in Node.js
The "Cannot find module" or "MODULE_NOT_FOUND" error is one of the most common Node.js errors. It means Node.js cannot locate the module you are trying to import.
Check if the Module Is Installed
Your package.json lists dependencies, but the node_modules folder must also contain them. Run npm install to install all dependencies. If the module is missing from package.json, install it with npm install
Verify the Import Path
For local files, ensure the path starts with ./ or ../ for relative imports. Absolute imports require specific configuration. Check that the file extension matches (.js, .mjs, .cjs, .json, .node). Node.js 16+ requires explicit extensions for ES modules.
Fix node_modules Issues
Delete node_modules and package-lock.json, then run npm install fresh. This fixes corrupted installations. Use npm ci for clean installs in CI environments. Check for permission issues on the node_modules folder.
Global Module Resolution
If you installed a module globally with npm install -g, Node.js may not find it unless NODE_PATH is set or you use npm link. Use npx to run globally installed packages without path issues.
TypeScript-Specific Module Issues
TypeScript requires @types/package-name for type definitions. Ensure tsconfig.json has proper moduleResolution set to "node" or "node16". For path aliases, configure paths and baseUrl in tsconfig.json.