express-favicon is a middleware for Express.js applications, designed to serve a favicon (favorite icon) for web browsers. It intercepts requests for `/favicon.ico` and responds with the specified icon file, preventing these requests from hitting other middleware or routes unnecessarily. The current stable version is 2.0.4, published in 2023. Given its last commit in 2017 and infrequent updates, it operates as a low-maintenance or effectively abandoned package. While functional for its basic purpose, it is less actively maintained and generally less feature-rich compared to the official `serve-favicon` middleware from the Express/Connect organization, which offers better caching and configuration options. It ships with built-in TypeScript types.
npm install express-faviconVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes an Express application and uses `express-favicon` to serve a favicon. It demonstrates proper ES module usage with `__dirname` emulation for file path resolution, and includes the middleware before any routes to ensure favicon requests are handled efficiently.
Install `express-favicon` or `serve-favicon` via npm (`npm install express-favicon`) and require/import it explicitly, then use `app.use(favicon(...))`.
Ensure `app.use(favicon(...))` is one of the very first middleware applied to your Express application instance.
Use `path.join(__dirname, 'path', 'to', 'favicon.ico')` to construct absolute paths reliably. For ES Modules, emulate `__dirname` using `path.dirname(fileURLToPath(import.meta.url))`.
Consider migrating to `serve-favicon` (`npm install serve-favicon`). It offers similar functionality with ongoing maintenance and potentially more robust caching and error handling.
Ensure `const express = require('express');` and `const app = express();` are correctly executed before calling `app.use()`.Verify that the path passed to `favicon()` is correct and points to an existing file. Ensure `app.use(favicon(...))` is placed early in your middleware chain, before `app.use(express.static(...))` or other routes.
Clear your browser's cache for the specific site or perform a hard refresh (Ctrl+F5 or Cmd+Shift+R). For development, try opening in an incognito window. Ensure `express-favicon` is configured to serve with public caching (which it does by default for 1 year, similar to `serve-favicon`).