koa-morgan is an HTTP request logger middleware for Koa, acting as a wrapper for the popular `morgan` package. It enables developers to log incoming requests and responses in various formats, supporting custom output streams for integration with logging infrastructure like file systems or external services. The latest stable version, 1.0.1, was published over 10 years ago, indicating a lack of active development or a very stable, feature-complete state. It specifically targets Koa v2.x applications, while older 0.x versions supported Koa v1. Its primary differentiator is providing the robust logging capabilities of `morgan` within the Koa middleware pattern. The package is not actively released and new versions are highly unlikely.
npm install koa-morganVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic setup of koa-morgan to log HTTP requests in 'combined' format to a file stream in a Koa v2 application.
For Koa v1 projects, explicitly install and pin `koa-morgan` to a `0.x.x` version. For Koa v2 projects, use `1.x.x` versions. Koa v3.x is not officially supported by `koa-morgan`.
For new projects or those requiring active support and modern features, consider alternative actively maintained Koa logging middleware or integrate the `morgan` package directly with a custom Koa middleware for greater control and future compatibility.
Always use `const morgan = require('koa-morgan')` for importing this package. If your project is strictly ESM, you might need to use a dynamic import (`await import('koa-morgan')`) or consider a different logging library with native ESM support.Ensure you are using the correct `koa-morgan` version for your Koa application (v1.x for Koa v2, v0.x for Koa v1). For `koa-morgan` v0.x, the API is `app.use(morgan.middleware(format, options))`.
While `koa-morgan` is a CommonJS module, this specific error indicates the *importer* is ESM. The most straightforward fix is to ensure the file importing `koa-morgan` is also CommonJS. If not possible, you might need to dynamically import it using `import('koa-morgan').then(module => module.default)`. However, `koa-morgan` might not work seamlessly in a pure ESM context due to its age.Ensure that the `fs` module is properly imported at the top of your file using `const fs = require('fs')` and that `accessLogStream` is correctly initialized before being passed to `koa-morgan` options. Verify file paths for write stream creation.