{"id":14716,"library":"morgan","title":"Morgan HTTP Logger","description":"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.","status":"active","version":"1.10.1","language":"javascript","source_language":"en","source_url":"https://github.com/expressjs/morgan","tags":["javascript","express","http","logger","middleware"],"install":[{"cmd":"npm install morgan","lang":"bash","label":"npm"},{"cmd":"yarn add morgan","lang":"bash","label":"yarn"},{"cmd":"pnpm add morgan","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[],"quickstart":{"code":"const express = require('express');\nconst morgan = require('morgan');\nconst app = express();\nconst port = process.env.PORT ?? 3000;\n\n// Use 'dev' format for concise colored output during development\napp.use(morgan('dev'));\n\napp.get('/', (req, res) => {\n  res.send('Hello World!');\n});\n\napp.get('/error', (req, res) => {\n  res.status(500).send('Something broke!');\n});\n\napp.listen(port, () => {\n  console.log(`Example app listening on port ${port}`);\n});","lang":"javascript","description":"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."},"warnings":[{"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.","message":"When 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`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid using `DEBUG_FD`. Consult the `debug` package documentation for alternative debugging configurations.","message":"The `DEBUG_FD` environment variable, used for debugging output stream, was deprecated in `morgan` v1.8.0.","severity":"deprecated","affected_versions":">=1.8.0"},{"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.","message":"The 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.","severity":"breaking","affected_versions":">=1.6.0"},{"fix":"Upgrade to `morgan` v1.9.1 or later to ensure proper handling of special characters in format strings.","message":"Prior to v1.9.1, using certain special characters in custom format strings could lead to incorrect logging or parsing issues.","severity":"gotcha","affected_versions":"<1.9.1"}],"env_vars":null,"last_verified":"2026-04-18T00:00:00.000Z","next_check":"2026-07-17T00:00:00.000Z","problems":[{"fix":"Use the CommonJS `require` syntax: `const morgan = require('morgan');`","cause":"Attempting to use ES module `import` syntax (`import morgan from 'morgan'`) in a CommonJS-only Node.js environment or for this CommonJS-only package.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Call `morgan` with a format string or custom function to create the middleware, for example: `app.use(morgan('dev'));`","cause":"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')`).","error":"TypeError: app.use() requires a middleware function but got a Object"}],"ecosystem":"npm"}