Last modified: Jun 30, 2025 By Alexander Williams
Fix Error: Cannot find module './app.js'
If you see the error Cannot find module './app.js', your Node.js app can't locate the file. This is a common issue with simple fixes.
Why This Error Occurs
The error means Node.js cannot find the app.js
file in your project. This happens for several reasons:
- The file doesn't exist in the specified path
- You're running the script from the wrong directory
- There's a typo in the file name or path
- The file extension is incorrect
How to Fix the Error
Follow these steps to resolve the issue.
1. Check if the File Exists
First, verify that app.js
exists in your project folder. Use this command:
ls ./app.js
If the file doesn't exist, you'll need to create it or restore it from backup.
2. Verify Your Current Directory
Run your script from the correct directory. Check your current location with:
pwd
Then navigate to your project folder if needed:
cd /path/to/your/project
3. Check for Typos
Ensure you're using the correct file name and path. Common mistakes include:
- Using
app
instead of./app.js
- Misspelling the file name (e.g.,
appp.js
) - Using wrong case (e.g.,
App.js
on case-sensitive systems)
4. Verify File Extensions
Node.js requires the full file name with extension. Make sure you're using:
require('./app.js');
Instead of:
require('./app'); // Might not work without extension
Example Scenario
Here's a common situation where this error occurs:
// server.js
const app = require('./app'); // Error if app.js doesn't exist
To fix it, either create app.js
or correct the path:
// Correct version
const app = require('./app.js');
Related Errors
Similar errors you might encounter include Cannot find module '../config' or Cannot find module './routes/index'. The solutions are often similar.
Prevention Tips
To avoid this error in the future:
- Always use full file paths with extensions
- Double-check file names before requiring them
- Use consistent naming conventions
- Consider using
path.join()
for file paths
Conclusion
The Cannot find module './app.js' error is easy to fix. Check the file exists, verify your path, and ensure correct naming. For similar issues like Cannot find module 'puppeteer', the approach is often the same.
Always pay attention to file paths and naming in your Node.js projects to prevent these errors.