Last modified: Jun 23, 2026
Fix Cannot Find Module 'morgan' Error
If you are working on a Node.js project and see the error "Cannot find module 'morgan'", don't panic. This is a common issue for beginners and experienced developers alike. It usually means the morgan package is missing from your project's dependencies. In this article, we will walk through the causes, solutions, and best practices to fix this error quickly.
What is Morgan?
Morgan is a popular HTTP request logger middleware for Node.js. It helps you see incoming requests, their status codes, and response times in the console. It is often used with Express.js to log API calls during development and production. When you try to import or require it without installing it first, Node.js throws the "Cannot find module" error.
Common Causes of This Error
There are a few reasons why you might see this error:
- Morgan is not installed in your project's
node_modulesfolder. - You forgot to run
npm installafter cloning a project from GitHub. - Morgan is listed in
devDependenciesbut you are running the app in production mode. - You are using a global install instead of a local one.
How to Fix the Error
1. Install Morgan Locally
The most common fix is to install Morgan as a project dependency. Open your terminal in the root folder of your Node.js project and run:
npm install morgan
This command adds Morgan to your node_modules folder and updates your package.json file. After installation, your error should be gone.
2. Run npm install If You Cloned a Project
If you just cloned a repository from GitHub, the node_modules folder is usually excluded. You need to install all dependencies listed in package.json. Run:
npm install
This will install everything, including Morgan if it is listed as a dependency. If the error persists, check if Morgan is in the dependencies section of your package.json file.
3. Check Your package.json
Open your package.json and look for Morgan under dependencies or devDependencies. If it is missing, add it manually and run npm install. Here is an example:
{
"dependencies": {
"express": "^4.18.0",
"morgan": "^1.10.0"
}
}
After editing, run npm install again to install the package.
4. Use the Correct Import Statement
Make sure you are importing Morgan correctly in your code. For CommonJS (default in Node.js), use require:
const morgan = require('morgan');
For ES modules (if you set "type": "module" in package.json), use import:
import morgan from 'morgan';
Using the wrong syntax can also cause module resolution errors.
5. Clear npm Cache (If Nothing Works)
Sometimes the npm cache gets corrupted. Try clearing it and reinstalling:
npm cache clean --force
rm -rf node_modules
npm install
This forces a fresh install of all dependencies, including Morgan.
Example: Using Morgan in an Express App
Here is a simple example of using Morgan in an Express application. This will help you confirm that everything works after fixing the error.
// app.js
const express = require('express');
const morgan = require('morgan'); // Import morgan
const app = express();
const port = 3000;
// Use morgan to log requests
app.use(morgan('dev'));
// Define a route
app.get('/', (req, res) => {
res.send('Hello World!');
});
// Start the server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
When you run this code with node app.js and visit http://localhost:3000, you will see logs in the terminal like this:
GET / 200 4.567 ms - 12
If you see this output, Morgan is working correctly.
What If You Still See the Error?
If you have tried all the steps above and the error persists, check the following:
- Node.js version: Make sure you have Node.js installed. Run
node -vto check. - Working directory: Ensure you are in the correct project folder when running commands.
- Global vs local: Avoid installing Morgan globally with
npm install -g morganunless you have a specific reason. Local installs are safer.
If you encounter similar errors with other modules, read our guide on how to Fix Node.js Error: Cannot find module for broader solutions.
Prevent This Error in the Future
To avoid this error in future projects, always follow these best practices:
- Always run
npm installafter cloning a repository. - Use a
package-lock.jsonfile to lock dependency versions. - Check your
package.jsonbefore pushing code to GitHub. - Add
node_modulesto your.gitignorefile.
Conclusion
The "Cannot find module 'morgan'" error is easy to fix. Most of the time, it is caused by a missing installation or a forgotten npm install. By following the steps in this article, you can resolve the error quickly and get back to building your Node.js applications. Remember to always check your package.json and run npm install when working with shared projects. For more help with module errors, check out our detailed guide on Fix Node.js Error: Cannot find module. Happy coding!