node-cookie is a utility library for Node.js environments, designed to simplify the parsing, signing, encryption, creation, and clearing of HTTP cookies. It directly interfaces with Node.js's built-in `http.IncomingMessage` and `http.ServerResponse` objects, making it agnostic to higher-level web frameworks. The current stable version is 2.1.2, which was last published over six years ago. Despite some recent activity on its GitHub repository (commits in 2022-2023), the package itself has not seen new releases, suggesting it is in an abandoned or very low-maintenance state. Its key differentiator is providing low-level, built-in cookie signing and encryption capabilities without requiring a full framework or complex middleware, directly leveraging Node.js crypto primitives.
npm install node-cookieVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting and retrieving signed and encrypted HTTP cookies using `node-cookie` with a basic Node.js HTTP server. It shows `create` for writing cookies with options for expiration, signing, and encryption, and `parse` and `get` for reading them, also handling decryption and unsigning.
For ESM projects, use `import nodeCookie from 'node-cookie';` and then access methods like `nodeCookie.create()`. For CommonJS, stick to `const nodeCookie = require('node-cookie');`.Generate a strong, random secret (e.g., 32 characters or more) and load it from environment variables at runtime (`process.env.COOKIE_SECRET`). Never commit secrets to version control.
Ensure `secure: true` is only used when the server is accessible via HTTPS. Double-check `domain` (should match the host or be a valid parent domain) and `path` (defaults to `/`) options to ensure they align with the client-side context. Test with browser developer tools to inspect `Set-Cookie` headers.
Consider migrating to actively maintained cookie parsing and signing libraries like `cookies` or `tough-cookie` for robust, up-to-date solutions, especially in production environments.
Ensure a non-empty string secret is passed as the fourth argument to `create` or `parse`/`get` methods, or as part of the options object.
Verify that the *exact same secret* is used for both encrypting (during `create`) and decrypting (during `parse` or `get`) a cookie. Ensure the secret meets the length requirements of the underlying cryptographic algorithm (e.g., 16, 24, or 32 characters for AES-128, AES-192, AES-256 respectively, or derived consistently).
Check the `Set-Cookie` header in the server response for errors. Ensure `secure: true` is only for HTTPS. Verify `domain` and `path` match the request URL. If `httpOnly: true` is set, the cookie won't be accessible by client-side JavaScript, but should still be sent by the browser. Explicitly set `expires` or `maxAge` to ensure persistence.
No dependency data recorded yet.