Express Favicon Middleware
raw JSON →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.
Common errors
error TypeError: app.use is not a function ↓
const express = require('express'); and const app = express(); are correctly executed before calling app.use(). error Cannot GET /favicon.ico (404 Not Found) ↓
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. error Favicon not appearing in browser tab (or old favicon persisting) ↓
express-favicon is configured to serve with public caching (which it does by default for 1 year, similar to serve-favicon). Warnings
breaking Express versions 4.x and above no longer bundle middleware like `favicon`. Users upgrading from older Express versions (e.g., 3.x) will find `express.favicon` undefined and must install `express-favicon` or `serve-favicon` separately. ↓
gotcha The middleware must be placed early in the Express middleware stack, typically before any logging middleware or main route handlers. If placed after routes or static file middleware, requests for `/favicon.ico` might be handled by other handlers, preventing `express-favicon` from doing its job or causing unintended logging. ↓
gotcha Path resolution for the favicon file can be tricky, especially in ES module contexts or when applications are bundled. Using `__dirname` directly in ESM files requires emulation, and incorrect paths will lead to a 404 for `/favicon.ico` or the server failing to start if the file is mandatory. ↓
deprecated This package, `express-favicon`, has not seen significant updates since 2017 (last commit) and 2023 (last npm publish, mostly metadata update) and is largely unmaintained. The officially recommended and actively maintained alternative for serving favicons in Express is `serve-favicon`. ↓
Install
npm install express-favicon yarn add express-favicon pnpm add express-favicon Imports
- favicon wrong
import { favicon } from 'express-favicon';correctimport favicon from 'express-favicon'; - favicon
const favicon = require('express-favicon'); - RequestHandler
import type { RequestHandler } from 'express';
Quickstart
import express from 'express';
import favicon from 'express-favicon';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
// Serve favicon.png from the 'public' directory. Middleware order is crucial.
// Make sure 'public' exists in your project root with a favicon.png inside.
app.use(favicon(path.join(__dirname, 'public', 'favicon.png')));
app.get('/', (req, res) => {
res.send('<h1>Hello, Express Favicon! Check your browser tab for the favicon.</h1>');
});
const PORT = process.env.PORT ?? 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
console.log('Ensure you have a favicon.png in a "public" directory next to your server file.');
});