Socket
Book a DemoInstallSign in
Socket

modern-cookies

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

modern-cookies

🍪 A Lightweight and Modern Cookie Utility for Express and Nest.js

1.0.2
latest
Source
npmnpm
Version published
Weekly downloads
357
158.7%
Maintainers
1
Weekly downloads
 
Created
Source
banner

modern-cookies

A Lightweight and Modern Cookie
Utility for Express and Nest.js

License npm version npm downloads stars

About 📖

modern-cookies is a simple, type-safe, and secure cookie management utility for Express and Nest.js applications.
It provides a minimal API for setting, getting, and deleting cookies — with security best practices built in.

Features 🌟

  • 💡 Simple API — Intuitive functions setCookie, getCookie, and deleteCookie for effortless cookie management.
  • 🔨 Built on Reliability — Uses the proven cookie library for RFC-compliant parsing and serialization.
  • Graceful Error Handling — Returns false on failures and provides a logError flag for optional console logging.
  • 🛡️ Security-Aware Defaults — Automatically enforces rules for special prefixes: __Secure- and __Host-.
  • ⚙️ Type-Safe & Cross-Compatible — Fully written in TypeScript with complete type definitions. Works in both ESM and CommonJS runtimes.

Installation 🔥

npm install modern-cookies@latest

💡 Works with npm, pnpm, and yarn. You can use it in dev dependencies since it's typically used only for local HTTPS.

Usage 🪛

Express 📫

import express from 'express';
import { getCookie, setCookie, deleteCookie } from 'modern-cookies';
import { env } from './env';

function bootstrap() {
  const app = express();

  app.use(express.json());
  app.use(express.urlencoded({ extended: true }));

  app.get('/get-cookie', (req, res) => {
    const cookieValue = getCookie(req, 'myCookie');
    res.json({ cookieValue });
  });

  app.get('/set-cookie', (req, res) => {
    const isSet = setCookie(res, 'myCookie', 'SomeValue123', {
      httpOnly: true,
      maxAge: 60, // 1 minute
    });

    res.json({ message: isSet ? 'Cookie set successfully' : 'Failed to set cookie' });
  });

  app.get('/delete-cookie', (req, res) => {
    const isDeleted = deleteCookie(res, 'myCookie');
    res.json({ message: isDeleted ? 'Cookie deleted successfully' : 'Failed to delete cookie' });
  });

  app.listen(env.PORT || 3000, () => {
    console.log(`🚀 Express server running on: http://localhost:${env.PORT || 3000}`);
  });
}

bootstrap();

NestJS 🪺

import { Controller, Get, Req, Res } from '@nestjs/common';
import type { Request, Response } from 'express';
import { getCookie, setCookie, deleteCookie } from 'modern-cookies';

@Controller('')
export class PublicController {
  @Get('get-cookie')
  getCookie(@Req() req: Request) {
    const cookieValue = getCookie(req, 'myCookie');
    return { cookieValue };
  }

  @Get('set-cookie')
  setCookie(@Res() res: Response) {
    const isSet = setCookie(res, 'myCookie', 'SomeValue123', {
      httpOnly: true,
      maxAge: 60, // 1 minute
    });
    // Since we used the `Res` we need to send the response manually
    res.json({ message: isSet ? 'Cookie set successfully' : 'Failed to set cookie' });
  }

  @Get('delete-cookie')
  deleteCookie(@Res() res: Response) {
    const isDeleted = deleteCookie(res, 'myCookie');
    res.json({ message: isDeleted ? 'Cookie deleted successfully' : 'Failed to delete cookie' });
  }
}

Credit 💪🏽

We want to thank Cookie NPM package for the cookie parsing and serialization used in this package.

Contributions 🤝

Want to contribute or suggest a feature?

  • Open an issue or feature request
  • Submit a PR to improve the packages or add new ones
  • Star ⭐ the repo if you like what you see

License 📜

This project is licensed under the MIT License.

Thank you!

Keywords

express

FAQs

Package last updated on 09 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.