{"id":17637,"library":"express-sitemap-xml","title":"Express Sitemap XML Middleware","description":"express-sitemap-xml is an Express.js middleware designed to automatically generate and serve sitemap.xml files based on a dynamic list of URLs provided by an async function. It intelligently handles large sitemaps, automatically splitting them into multiple files and generating a sitemap index (sitemap.xml) when more than 50,000 URLs are present, adhering to the sitemap protocol. The current stable version is 3.1.0, with recent updates indicating active maintenance and feature additions, such as image support and base URL fixes. It differentiates itself by its automatic handling of large sitemaps, caching mechanisms for performance, and a flexible API that can also be used independently of Express for pure sitemap generation. The package calls the URL-fetching function at most once every 24 hours, caching the results to optimize performance for subsequent requests.","status":"active","version":"3.1.0","language":"javascript","source_language":"en","source_url":"git://github.com/feross/express-sitemap-xml","tags":["javascript","express","google","serve sitemap","serve sitemap.xml","site map","site map xml","sitemap","sitemap generator"],"install":[{"cmd":"npm install express-sitemap-xml","lang":"bash","label":"npm"},{"cmd":"yarn add express-sitemap-xml","lang":"bash","label":"yarn"},{"cmd":"pnpm add express-sitemap-xml","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for using the primary middleware functionality.","package":"express","optional":false}],"imports":[{"note":"The default export is the middleware function. CommonJS users should use `const expressSitemapXml = require('express-sitemap-xml')`.","wrong":"const { expressSitemapXml } = require('express-sitemap-xml')","symbol":"expressSitemapXml","correct":"import expressSitemapXml from 'express-sitemap-xml'"},{"note":"This is a named export for generating sitemaps without Express. It's not the default export.","wrong":"import buildSitemaps from 'express-sitemap-xml'","symbol":"buildSitemaps","correct":"import { buildSitemaps } from 'express-sitemap-xml'"},{"note":"URLs can be strings or objects with `url`, `lastMod`, and `changeFreq` properties. 'priority' is ignored by Google and not supported.","symbol":"URL object options","correct":"await getUrlsFromDatabase(); return [{ url: '/page', lastMod: new Date(), changeFreq: 'weekly' }];"}],"quickstart":{"code":"import express from 'express';\nimport expressSitemapXml from 'express-sitemap-xml';\n\nconst app = express();\n\n// Simulate fetching URLs from a database\nasync function getUrlsFromDatabase() {\n  // In a real application, you would fetch these dynamically\n  console.log('Fetching URLs for sitemap...');\n  return [\n    '/',\n    '/about',\n    '/contact',\n    { url: '/products', lastMod: new Date(), changeFreq: 'daily' },\n    { url: '/blog/post-1', lastMod: new Date('2023-01-15T10:00:00Z'), changeFreq: 'monthly' }\n  ];\n}\n\n// Use the sitemap middleware\n// The getUrls function will be called at most once every 24 hours.\napp.use(expressSitemapXml(getUrlsFromDatabase, 'https://example.com'));\n\n// Other Express routes\napp.get('/', (req, res) => res.send('Welcome to the homepage!'));\napp.get('/about', (req, res) => res.send('About Us'));\n\nconst PORT = process.env.PORT || 3000;\napp.listen(PORT, () => {\n  console.log(`Server running on http://localhost:${PORT}`);\n  console.log('Sitemap available at http://localhost:3000/sitemap.xml');\n  console.log('Remember to add \\\"Sitemap: https://example.com/sitemap.xml\\\" to your robots.txt');\n});\n","lang":"typescript","description":"This quickstart sets up an Express server with the express-sitemap-xml middleware, demonstrating how to provide a dynamic list of URLs and serve the sitemap.xml file. It includes a simulated asynchronous URL fetching function and shows both simple URL strings and more detailed URL objects."},"warnings":[{"fix":"Consider application restart for immediate updates or manually clear caches if a more granular control is needed, acknowledging the 24-hour cache limit. For most use cases, this caching behavior is beneficial.","message":"The `getUrls` function passed to `expressSitemapXml` is called at most once every 24 hours. If your URLs change more frequently and you need the sitemap to reflect those changes immediately, you'll need to restart your application or implement a custom cache invalidation mechanism outside of the middleware's built-in caching.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Do not include a `priority` field in URL objects. Focus on `lastMod` and `changeFreq` if you need to provide additional details beyond the URL itself.","message":"When providing URLs as objects, the `priority` option is explicitly not supported. Google states that it ignores the `priority` value in sitemaps, so the package omits this option to avoid confusion and unnecessary data.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always point your `robots.txt` to the root `sitemap.xml` (e.g., `Sitemap: https://yourdomain.com/sitemap.xml`). The package handles the internal linking to sub-sitemaps automatically.","message":"For sitemaps with more than 50,000 URLs, the package automatically generates a sitemap index (`sitemap.xml`) and multiple sitemap files (`sitemap-0.xml`, `sitemap-1.xml`, etc.). Ensure your `robots.txt` points to the main `sitemap.xml` file, as it will act as the index.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"If using ES modules: `import expressSitemapXml from 'express-sitemap-xml';`. If using CommonJS: `const expressSitemapXml = require('express-sitemap-xml');`.","cause":"Incorrect import statement or mixing CommonJS `require` with ES module `import` syntax for the default export.","error":"TypeError: expressSitemapXml is not a function"},{"fix":"Ensure the first argument is a function, preferably an `async` function, for example: `app.use(expressSitemapXml(async () => ['/'], 'https://example.com'))`.","cause":"The first argument to `expressSitemapXml` must be an async function that returns an array of URLs.","error":"Error: Argument `getUrls` must be a function."},{"fix":"Switch to ES module `import` syntax: `import express from 'express'; import expressSitemapXml from 'express-sitemap-xml';`.","cause":"Attempting to use `require()` in a project configured for ES modules (e.g., `\"type\": \"module\"` in `package.json`).","error":"ReferenceError: require is not defined in ES module scope"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}