{"id":17548,"library":"connect-flash","title":"Flash Message Middleware for Connect and Express","description":"connect-flash is a middleware for Node.js applications, primarily used with Connect and Express frameworks, to provide transient flash messages. These messages are stored in the user's session and are cleared after being displayed, making them ideal for user feedback during redirects (e.g., \"Successfully logged in!\" or \"Invalid credentials.\"). Originally extracted from Express 2.x for use with Express 3.x after direct flash support was removed, the package is quite old, with its latest version (0.1.1) published in 2013. Despite its age and lack of active maintenance, it continues to be utilized by many projects, often requiring adaptation for modern Express (4.x+) applications. It differentiates itself through its simplicity and direct integration with session middleware, offering a straightforward API for managing messages across HTTP requests.","status":"abandoned","version":"0.1.1","language":"javascript","source_language":"en","source_url":"git://github.com/jaredhanson/connect-flash","tags":["javascript","connect","express","flash","messages"],"install":[{"cmd":"npm install connect-flash","lang":"bash","label":"npm"},{"cmd":"yarn add connect-flash","lang":"bash","label":"yarn"},{"cmd":"pnpm add connect-flash","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for storing flash messages in the session. connect-flash relies entirely on an active session.","package":"express-session","optional":false},{"reason":"Often needed alongside express-session for parsing cookies, especially in older Express setups. Modern express-session can often handle this internally but explicit cookie-parser usage is common for compatibility or specific needs.","package":"cookie-parser","optional":false}],"imports":[{"note":"While originally designed for CommonJS (`require`), some users attempt ES Modules (`import`). The package itself does not officially support ESM, and direct `import` may lead to issues or require specific `type: module` configurations and transpilation. For most setups, stick to CommonJS.","wrong":"const flash = require('connect-flash');","symbol":"flash","correct":"import flash from 'connect-flash';"},{"note":"The `flash` middleware adds the `flash()` method to the `req` object. It can be called with two arguments (key, value) to set a message, or with one argument (key) to retrieve and clear messages of that type. Calling with no arguments retrieves all messages.","symbol":"req.flash()","correct":"req.flash('success', 'Operation successful!');\nconst messages = req.flash('info');"}],"quickstart":{"code":"import express from 'express';\nimport session from 'express-session';\nimport cookieParser from 'cookie-parser';\nimport flash from 'connect-flash';\nimport path from 'path';\nimport { fileURLToPath } from 'url';\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\nconst app = express();\nconst PORT = process.env.PORT || 3000;\n\n// Session setup must come BEFORE connect-flash\napp.use(cookieParser('keyboard cat')); // Use a strong secret in production\napp.use(session({\n  secret: 'another secret string',\n  resave: false,\n  saveUninitialized: true,\n  cookie: { maxAge: 60000 * 10 } // 10 minutes\n}));\n\n// Initialize connect-flash\napp.use(flash());\n\n// Make flash messages available in res.locals for templates\napp.use((req, res, next) => {\n  res.locals.success_msg = req.flash('success');\n  res.locals.error_msg = req.flash('error');\n  res.locals.info_msg = req.flash('info');\n  next();\n});\n\napp.set('views', path.join(__dirname, 'views'));\napp.set('view engine', 'ejs'); // Example with EJS\n\napp.get('/', (req, res) => {\n  res.render('index', { title: 'Home' });\n});\n\napp.get('/set-flash', (req, res) => {\n  req.flash('success', 'This is a success message!');\n  req.flash('info', 'Information to note.');\n  res.redirect('/show-flash');\n});\n\napp.get('/show-flash', (req, res) => {\n  // Messages are automatically available in res.locals due to middleware above\n  res.render('show', { title: 'Flash Messages' });\n});\n\napp.listen(PORT, () => {\n  console.log(`Server running on http://localhost:${PORT}`);\n});\n\n// You would also need 'views/index.ejs' and 'views/show.ejs' templates:\n/* views/index.ejs */\n// <h1><%= title %></h1>\n// <a href=\"/set-flash\">Set Flash Messages</a>\n\n/* views/show.ejs */\n// <h1><%= title %></h1>\n// <% if (success_msg.length) { %>\n//   <div class=\"alert alert-success\"><%= success_msg %></div>\n// <% } %>\n// <% if (info_msg.length) { %>\n//   <div class=\"alert alert-info\"><%= info_msg %></div>\n// <% } %>\n// <% if (error_msg.length) { %>\n//   <div class=\"alert alert-danger\"><%= error_msg %></div>\n// <% } %>\n// <a href=\"/\">Go Home</a>\n","lang":"javascript","description":"This quickstart demonstrates setting up connect-flash with modern Express (4.x+), including `cookie-parser` and `express-session`, and then setting and retrieving flash messages which are made available to EJS templates via `res.locals`."},"warnings":[{"fix":"Migrate to `cookie-parser` and `express-session` npm packages. Install them (`npm install cookie-parser express-session`) and use them as separate middleware: `app.use(cookieParser('secret')); app.use(session({ ... }));`","message":"The original usage examples with `app.configure`, `express.cookieParser`, and `express.session` are deprecated and removed in Express 4.x and later. These must be replaced with separate `cookie-parser` and `express-session` middleware.","severity":"breaking","affected_versions":"<=0.1.1 (used with old Express versions)"},{"fix":"For projects using ESM, it is generally safer to transpile connect-flash or ensure compatibility through tools like Babel. If possible, consider alternative, actively maintained flash message libraries with explicit ESM support or stick to CommonJS for connect-flash integration.","message":"connect-flash does not officially support ES Modules (ESM). Attempting to use `import flash from 'connect-flash';` directly in an ESM project may lead to runtime errors or `req.flash` being undefined, especially if `type: \"module\"` is set in `package.json`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `express-session` (and `cookie-parser` if used separately) is initialized and placed in the middleware chain *before* `connect-flash`. For example: `app.use(cookieParser(...)); app.use(session(...)); app.use(flash());`","message":"Flash messages are stored in the session. If `express-session` middleware is not correctly configured or is placed *after* `connect-flash` middleware, `req.flash` will not be available, leading to runtime errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For new projects, consider using a more modern and actively maintained flash message solution or implementing a custom, simple session-based message system. For existing projects, be aware of potential vulnerabilities or compatibility issues with newer Node.js or Express versions.","message":"The `connect-flash` package is effectively abandoned, with its last update 13 years ago. While still functional, it receives no security updates or new features.","severity":"deprecated","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Ensure `express-session` and `cookie-parser` (if used) are loaded before `connect-flash`. Example: `app.use(cookieParser('secret')); app.use(session({ secret: '...', resave: false, saveUninitialized: true })); app.use(flash());`","cause":"`connect-flash` middleware has not been applied, or `express-session` middleware (which `connect-flash` depends on) was not initialized or was placed after `connect-flash` in the middleware chain.","error":"TypeError: req.flash is not a function"},{"fix":"Ensure `req.flash()` is called exactly once to retrieve messages for display, typically in a middleware that populates `res.locals` before rendering. Verify that your template correctly accesses the `res.locals` variables. Also check if a redirect is happening before `res.locals` are set or if the session is correctly configured.","cause":"Flash messages are cleared after being retrieved. If `req.flash()` is called multiple times for the same key, or if `res.locals` assignment is done incorrectly, messages might be consumed before the view can render them.","error":"Flash messages do not appear or are empty in the view."},{"fix":"Remove `app.configure` blocks. Apply middleware directly to the `app` object without conditional configuration blocks, as Express 4.x+ supports a unified middleware chain. E.g., replace `app.configure(function() { app.use(...) });` with `app.use(...);`","cause":"`app.configure` was deprecated in Express 3.x and completely removed in Express 4.x. `connect-flash` examples from its original README use this syntax.","error":"ReferenceError: app.configure is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}