Last modified: Jun 30, 2025 By Alexander Williams
Fix Error: Cannot find module 'socket.io'
If you see the error Cannot find module 'socket.io', don't worry. This is a common issue in Node.js projects. It usually means the module isn't installed or there's a path problem.
Why does this error occur?
The error appears when Node.js can't locate the socket.io module. This happens for several reasons:
1. The module isn't installed in your project.
2. It's installed in the wrong directory.
3. There are permission issues.
4. The package.json file is missing the dependency.
Solution 1: Install socket.io
The simplest fix is to install the module. Run this command in your project directory:
npm install socket.io
If you need it as a development dependency, use:
npm install socket.io --save-dev
Solution 2: Check your package.json
Ensure socket.io is listed in your dependencies. Your package.json should include:
"dependencies": {
"socket.io": "^4.0.0"
}
After adding it, run npm install
to install all dependencies.
Solution 3: Verify the installation path
Sometimes modules install in the wrong place. Check where Node.js looks for modules:
console.log(module.paths);
Ensure your project's node_modules folder is in this list.
Solution 4: Global vs local installation
Avoid global installations for project-specific modules. If you installed socket.io globally, remove it:
npm uninstall -g socket.io
Then install it locally in your project.
Solution 5: Clear npm cache
A corrupted cache can cause installation issues. Clear it with:
npm cache clean --force
Then reinstall your modules.
Solution 6: Check file permissions
Permission issues can prevent module access. On Linux/Mac, try:
sudo chown -R $(whoami) node_modules
Solution 7: Recreate node_modules
Sometimes the entire node_modules folder gets corrupted. Delete it and reinstall:
rm -rf node_modules package-lock.json
npm install
Solution 8: Verify require statement
Ensure your code correctly requires socket.io:
const io = require('socket.io')();
// or for ES modules:
import { Server } from 'socket.io';
Common mistakes to avoid
1. Misspelling the module name in require()
2. Using different versions in client and server
3. Not restarting your server after installation
If you're facing similar errors with other modules, check our guides on Fix Error: Cannot find module 'webpack' or Fix Error: Cannot find module 'react'.
Conclusion
The Cannot find module 'socket.io' error is usually easy to fix. Most often, it just requires proper installation. Follow these steps methodically to resolve the issue.
Remember to check your Node.js and npm versions are up to date. For more complex issues, similar solutions apply as in our TypeScript module error guide.
With these solutions, you should have socket.io working in your project quickly. Happy coding!