Registry / devops / koa-session2

koa-session2

JSON →
library2.2.10jsnpmunverified

Middleware for Koa2 to get/set session with custom stores like Redis or MongoDB. Current stable version is 2.2.10, with infrequent releases. Uses native ES6 async/await (Node >=7.6.0). Supports custom store classes (extending Store) and session refresh. Less active than koa-session but offers a simpler Store interface.

npm install koa-session2
INSTALL
IMPORT
SIG · KOA-SESSION2
K
koa-session2
devopsjavascriptv2.2.10
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

session
const session = require('koa-session2');
import session from 'koa-session2';
Package is CommonJS; ESM import may fail in some setups.
Store
const { Store } = require('koa-session2');
const Store = require('koa-session2').Store;
Named export for custom store base class.
default
const session = require('koa-session2');
import session from 'koa-session2';
Default export is the session middleware function.

Basic session middleware setup with a view counter example.

const Koa = require('koa'); const session = require('koa-session2'); const app = new Koa(); app.use(session({ key: 'SESSIONID', maxAge: 86400000 })); app.use(ctx => { if (!ctx.session.views) { ctx.session.views = 0; } ctx.session.views++; ctx.body = `Views: ${ctx.session.views}`; }); app.listen(3000);
Debug
Known issues
gotchaThe Store base class methods #get(sid, ctx), #set(session, opts, ctx), #destroy(sid, ctx) may receive ctx as second argument; check documentation for signature.
fix
Implement methods with correct signature: get(sid, ctx), set(session, { sid, maxAge }, ctx), destroy(sid, ctx)
affects: all
deprecatedPackage is in maintenance mode; no major updates since 2019. Consider using koa-session or other actively maintained alternatives.
fix
Migrate to koa-session (koa-session) or @koa/session
affects: >=2.0.0
breakingNode.js >=7.6.0 required; earlier versions will throw syntax errors due to async/await.
fix
Upgrade Node.js to >=7.6.0 or use older version from babel/node6 branches
affects: >=2.0.0
gotchaSession data is stored in memory by default; must provide custom store for persistence across restarts.
fix
Use a custom store like RedisStore as shown in README
affects: all
Errors
Common errors & fixes
ReferenceError: regeneratorRuntime is not defined
Using async/await without proper transpilation in older Node versions.
fix
Upgrade Node to >=7.6.0 or use babel polyfill
TypeError: session is not a function
Using import/require incorrectly; likely importing default as named.
fix
Use const session = require('koa-session2');
Error: Cannot find module '.../Store'
Store class not exported or path incorrect.
fix
Check export: module.exports = RedisStore; or use { Store } from 'koa-session2'
Upgrade
Version history
2.2.10latest on npm
Audit
Dependencies
koarequiredpeer dependency; koa-session2 is a Koa middleware
Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources