# nestjs-cookie-session

> Idiomatic NestJS module for cookie session

Latest version **5.0.0** (published 2026-08-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-cookie-session
pnpm add nestjs-cookie-session
yarn add nestjs-cookie-session
bun add nestjs-cookie-session
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2026-08-31 |
| First published | 2019-10-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22.12.0 |
| Dependencies | 1 |
| Unpacked size | 11.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 54 |
| Author | iamolegga |
| Maintainers | iamolegga |
| Keywords | nest, nestjs, nest.js, session, cookie, cookie-session |

## Links

- npm: https://www.npmjs.com/package/nestjs-cookie-session
- Repository: https://github.com/iamolegga/nestjs-cookie-session
- Homepage: https://github.com/iamolegga/nestjs-cookie-session#readme
- Issues: https://github.com/iamolegga/nestjs-cookie-session/issues
- npm.io page: https://npm.io/package/nestjs-cookie-session

## Dependencies (1)

- [create-nestjs-middleware-module](https://npm.io/package/create-nestjs-middleware-module.md) ^0.5.0

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 5.0.0 (latest) — 2026-08-31
- 4.0.0-alpha.2cdb2e (alpha) — 2026-08-31
- 4.0.0-alpha.0de25f — 2025-01-24
- 4.0.0 — 2025-01-23
- 3.0.1-alpha.995938 — 2025-01-23
- 3.0.1-alpha.f40f3b — 2024-11-22
- 3.0.1 — 2023-08-17
- 3.0.0-alpha.19d186 — 2023-08-17
- 3.0.0 — 2023-08-16
- 2.0.1-alpha.9ed7ee — 2023-08-16
- 2.0.1-alpha.24e3c1 — 2023-03-06
- 2.0.1-alpha.3ff414 — 2022-12-05
- 2.0.1-alpha.78a261 — 2022-06-15
- 2.0.1-alpha.29aad3 — 2022-06-15
- 2.0.1-alpha.002dbf — 2022-06-15
- … 269 more at https://npm.io/package/nestjs-cookie-session/versions

## README

<h1 align="center">nestjs-cookie-session</h1>

<p align="center">
  <a href="https://www.npmjs.com/package/nestjs-cookie-session">
    <img alt="npm" src="https://img.shields.io/npm/v/nestjs-cookie-session" />
  </a>
  <a href="https://www.npmjs.com/package/nestjs-cookie-session">
    <img alt="npm" src="https://img.shields.io/npm/dm/nestjs-cookie-session" />
  </a>
  <a href="https://github.com/iamolegga/nestjs-cookie-session/actions">
    <img alt="GitHub branch checks state" src="https://badgen.net/github/checks/iamolegga/nestjs-cookie-session/master">
  </a>
  <a href="https://qlty.sh/gh/iamolegga/projects/nestjs-cookie-session">
    <img src="https://qlty.sh/gh/iamolegga/projects/nestjs-cookie-session/coverage.svg" alt="Code Coverage" />
  </a>
  <a href="https://snyk.io/test/github/iamolegga/nestjs-cookie-session">
    <img alt="Known Vulnerabilities" src="https://snyk.io/test/github/iamolegga/nestjs-cookie-session/badge.svg" />
  </a>
  <a href="https://libraries.io/npm/nestjs-cookie-session">
    <img alt="Libraries.io" src="https://img.shields.io/librariesio/release/npm/nestjs-cookie-session">
  </a>
  <img alt="Dependabot" src="https://badgen.net/github/dependabot/iamolegga/nestjs-cookie-session">
  <img alt="Supported platforms: Express" src="https://img.shields.io/badge/platforms-Express-green" />
</p>

<p align="center">Idiomatic Cookie Session Module for NestJS. Built on top of <a href="https://npm.im/cookie-session">cookie-session</a> 😎</p>

This module implements a session with storing data directly in `Cookie`.

If you want to store data in one of [external stores](https://github.com/expressjs/session#compatible-session-stores) and passing ID of session to client via `Cookie`/`Set-Cookie` headers, you can look at [nestjs-session](https://github.com/iamolegga/nestjs-session).

## Example

Register module:

```ts
// app.module.ts
import { Module } from '@nestjs/common';
import {
  NestCookieSessionOptions,
  CookieSessionModule,
} from 'nestjs-cookie-session';
import { ViewsController } from './views.controller';

@Module({
  imports: [
    // sync params:

    CookieSessionModule.forRoot({
      session: { secret: 'keyboard cat' },
    }),

    // or async:

    CookieSessionModule.forRootAsync({
      imports: [ConfigModule],
      inject: [Config],
      //              TIP: to get autocomplete in return object
      //                  add `NestCookieSessionOptions` here ↓↓↓
      useFactory: async (config: Config): Promise<NestCookieSessionOptions> => {
        return {
          session: { secret: config.secret },
        };
      },
    }),
  ],
  controllers: [ViewsController],
})
export class AppModule {}
```

Use in controllers with NestJS built-in `Session` decorator:

```ts
// views.controller.ts
import { Controller, Get, Session } from '@nestjs/common';

@Controller('views')
export class ViewsController {
  @Get()
  getViews(@Session() session: { views?: number }) {
    session.views = (session.views || 0) + 1;
    return session.views;
  }
}
```

To run examples:

```sh
git clone https://github.com/iamolegga/nestjs-cookie-session.git
cd nestjs-cookie-session
npm i
npm run build
cd example
npm i
npm start
```

---

<p align="center"><b>This is the documentation for v5. Compatibility with earlier versions:</b></p>

| nestjs-cookie-session | NestJS       | Node.js |
| --------------------- | ------------ | ------- |
| v5                    | 11.2+, 12    | >=22.12 |
| [v4](https://github.com/iamolegga/nestjs-cookie-session/tree/4.0.0#readme) | 8, 9, 10, 11 | >=18 |

---

## Install

```sh
npm i nestjs-cookie-session cookie-session @types/cookie-session
```

## API

### CookieSessionModule

`CookieSessionModule` class has two static methods, that returns `DynamicModule`, that you need to import:

- `CookieSessionModule.forRoot` for sync configuration without dependencies
- `CookieSessionModule.forRootAsync` for sync/async configuration with dependencies

### CookieSessionModule.forRoot

Accept `NestCookieSessionOptions`. Returns NestJS `DynamicModule` for import.

### CookieSessionModule.forRootAsync

Accept `NestCookieSessionAsyncOptions`. Returns NestJS `DynamicModule` for import.

### NestCookieSessionOptions

`NestCookieSessionOptions` is the interface of all options, has next properties:

- `session` - **required** - [cookie-session options](https://github.com/expressjs/cookie-session#options).
- `forRoutes` - **optional** - same as NestJS built-in `MiddlewareConfigProxy['forRoutes']` [See examples in official docs](https://docs.nestjs.com/middleware#applying-middleware). Specify routes, that should have access to session. If `forRoutes` and `exclude` will not be set, then sessions will be set to all routes.
- `exclude` - **optional** - same as NestJS built-in `MiddlewareConfigProxy['exclude']` [See examples in official docs](https://docs.nestjs.com/middleware#applying-middleware). Specify routes, that should not have access to session. If `forRoutes` and `exclude` will not be set, then sessions will be set to all routes.

### NestCookieSessionAsyncOptions

`NestCookieSessionOptions` is the interface of options to create cookie session module, that depends on other modules, has next properties:

- `imports` - **optional** - modules, that cookie session module depends on. See [official docs](https://docs.nestjs.com/modules).
- `inject` - **optional** - providers from `imports`-property modules, that will be passed as arguments to `useFactory` method.
- `useFactory` - **required** - method, that returns `NestCookieSessionOptions`.

## Migration

### v5

Requires NestJS 11.2 or 12 and Node.js >=22.12. The package is now published
from `dist/` with an `exports` map instead of copying the build output into the
package root, so deep imports such as `nestjs-cookie-session/index` no longer
resolve — import from the package root.

The default route changed from `*` to `{/*splat}`, which fixes the global
prefix root. With `app.setGlobalPrefix('v1')` and no explicit `forRoutes`:

| request | before | after |
| -------------- | -------------- | ------- |
| `/v1` | **no session** | session |
| `/v1/anything` | session | session |

NestJS converted the old `*` into `/v1/{*path}`, which matches everything under
the prefix but not the prefix itself. Nothing changes if you pass your own
`forRoutes`, or if you do not set a global prefix; paths excluded from the
global prefix keep the session middleware as before.

### v2

`cookie-session` and `@types/cookie-session` are moved to peer dependencies, so you can update them independently.

<h2 align="center">Do you use this library?<br/>Don't be shy to give it a star! ★</h2>

<h3 align="center">Also if you are into NestJS you might be interested in one of my <a href="https://github.com/iamolegga#nestjs">other NestJS libs</a>.</h3>

---
_Source: https://npm.io/package/nestjs-cookie-session · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
