koa-session is a robust session middleware designed for the Koa.js web framework, enabling the management of user sessions within Koa applications. By default, it stores session data directly in HTTP cookies, offering a straightforward approach for many use cases. However, it also provides extensive support for integrating external session stores, which is crucial for overcoming limitations associated with client-side cookie storage, such as size constraints and potential security concerns from unencrypted client-side data. The current stable version is 7.0.2, released in January 2025, demonstrating active development and maintenance with recent bug fixes following major releases. Version 7.0.0 notably introduced dual CommonJS and ES module support and raised the minimum Node.js requirement to 18.19.0. Its key differentiators include deep integration into the Koa ecosystem, a highly configurable API for cookie options, and extensible hooks for custom session validation and pre-save logic, making it a versatile solution for session management in modern Koa applications.
npm install koa-sessionVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates a basic Koa application using koa-session to track page views, illustrating essential setup with cookie-based sessions, `app.keys` configuration, and common session options.
Upgrade your Node.js environment to version 18.19.0 or newer. If an upgrade is not possible, use `koa-session` version 6.x or earlier.
To maintain compatibility with old sessions, set the `key` option in your session configuration to `koa:sess` (e.g., `key: 'koa:sess'`). For new deployments, `koa.sess` is recommended.
Configure an external `store` option for `koa-session` to persist session data on the server. Implement a custom store or use a community-maintained store package (e.g., `koa-redis-store`).
Ensure `app.keys` is an array of secret strings (e.g., `app.keys = ['very secret key', 'another secret key']`) or a single string. Use strong, randomly generated keys and manage them securely (e.g., via environment variables).
In your `koa-session` configuration, set `secure: true` and `sameSite: 'lax'` (or `'strict'` depending on your needs) for production. Example: `secure: process.env.NODE_ENV === 'production', sameSite: 'lax'`.
If your project is an ES module, use `import session from 'koa-session';`. If your project is CommonJS and you are experiencing this with a dependency, ensure `koa-session` is compatible with your other dependencies or configure your build system for interop.
Before `app.use(session(CONFIG, app));`, add `app.keys = ['your_strong_secret_key'];` with one or more secure, randomly generated keys.
To fix this and maintain existing sessions, manually set the `key` option in your session configuration back to the old default: `{ key: 'koa:sess', ... }`.Ensure `app.keys` is correctly set and consistent across restarts. Check your session data for any unusual characters. If using an external store, verify its data integrity. Also, review any custom `encode`/`decode` functions for errors.
No dependency data recorded yet.