Registry / http-networking / morgan

morgan

JSON →
library1.10.1jsnpmunverified

Morgan is an HTTP request logger middleware for Node.js, often used with the Express framework. It allows developers to log request details such as method, URL, status, and response time in various predefined or custom formats. The current stable version is 1.10.1. Releases occur infrequently, with the latest update focusing on minor fixes and dependency updates, indicating a mature and stable library.

npm install morgan
INSTALL
IMPORT
SIG · MORGAN
M
morgan
http-networkingjavascriptv1.10.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

This quickstart sets up a basic Express.js application and integrates Morgan middleware using the 'dev' format for logging HTTP requests. It demonstrates how Morgan logs incoming requests and their responses, including status codes and response times.

const express = require('express'); const morgan = require('morgan'); const app = express(); const port = process.env.PORT ?? 3000; // Use 'dev' format for concise colored output during development app.use(morgan('dev')); app.get('/', (req, res) => { res.send('Hello World!'); }); app.get('/error', (req, res) => { res.status(500).send('Something broke!'); }); app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
Debug
Known issues
gotchaWhen using the `immediate: true` option, logs are written on request instead of response. This means response-specific data (like `:status` or `:res[content-length]`) will not be available in the log line, or may appear as `undefined`.
fix
Only use `immediate: true` if you specifically need logs to be written even if the server crashes before sending a response, and you don't require response-specific tokens. Otherwise, omit the option for default behavior.
affects: All versions
deprecatedThe `DEBUG_FD` environment variable, used for debugging output stream, was deprecated in `morgan` v1.8.0.
fix
Avoid using `DEBUG_FD`. Consult the `debug` package documentation for alternative debugging configurations.
affects: >=1.8.0
breakingThe behavior of the `:response-time` token changed in v1.6.0. Previously, it might have included response latency (the time taken to *send* the response). From v1.6.0 onwards, it strictly measures the time from request start to when response headers are sent.
fix
If your application relies on the old `:response-time` behavior, be aware of this change and adjust your logging interpretation or custom token implementation accordingly. Upgrade to v1.6.0+ for the more accurate measurement.
affects: >=1.6.0
gotchaPrior to v1.9.1, using certain special characters in custom format strings could lead to incorrect logging or parsing issues.
fix
Upgrade to `morgan` v1.9.1 or later to ensure proper handling of special characters in format strings.
affects: <1.9.1
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Attempting to use ES module `import` syntax (`import morgan from 'morgan'`) in a CommonJS-only Node.js environment or for this CommonJS-only package.
fix
Use the CommonJS `require` syntax: `const morgan = require('morgan');`
TypeError: app.use() requires a middleware function but got a Object
This error typically occurs in Express.js when you pass `morgan` (the module itself) directly to `app.use()` instead of calling it to get the middleware function (e.g., `morgan('dev')`).
fix
Call `morgan` with a format string or custom function to create the middleware, for example: `app.use(morgan('dev'));`
Upgrade
Version history
1.10.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
2
Resources
morgan — npm install morgan · libregistry