Sitemap Webpack Plugin
raw JSON → 1.1.1 verified Sat Apr 25 auth: no javascript
Webpack plugin to generate a sitemap.xml from a list of paths. Version 1.1.1 supports webpack 4 and 5 (for webpack <=3, use 0.5.x). It relies on the 'sitemap' library, supports gzipped output by default, and offers path-specific options like lastmod, priority, and changefreq. TypeScript types are included. Released under MIT license with weekly npm downloads around 6k.
Common errors
error TypeError: SitemapPlugin is not a constructor ↓
cause Using incorrect import style (named import instead of default) in CommonJS.
fix
const SitemapPlugin = require('sitemap-webpack-plugin').default;
error Error: Webpack version 3 not supported. Use version 0.5.x of sitemap-webpack-plugin. ↓
cause Using version 1.x with webpack 3.
fix
Install sitemap-webpack-plugin@0.5.1: npm install sitemap-webpack-plugin@0.5.1 --save-dev
error Module not found: Can't resolve 'sitemap' ↓
cause Missing peer dependency 'sitemap'.
fix
npm install sitemap
Warnings
breaking Version 1.x requires webpack 4 or 5. For webpack <=3, use version 0.5.x. ↓
fix Use sitemap-webpack-plugin@0.5.1 for webpack 3 or lower.
gotcha The constructor expects options as the third argument, not as part of the first object. Many users mistakenly pass options inside the first argument. ↓
fix Use new SitemapPlugin({ base, paths, options: { filename: 'custom.xml' } })
deprecated The 'skipgzip' option defaults to false. In older versions, gzip was always generated; now you can skip it explicitly. ↓
fix Set skipgzip: true if you do not want a .xml.gz file.
gotcha The 'lastmod' option set to boolean 'true' uses the current date. Provide a string for static dates. ↓
fix Use lastmod: '2023-01-01' for a fixed date, or lastmod: true for current date.
Install
npm install sitemap-webpack-plugin yarn add sitemap-webpack-plugin pnpm add sitemap-webpack-plugin Imports
- SitemapPlugin wrong
const SitemapPlugin = require('sitemap-webpack-plugin')correctimport SitemapPlugin from 'sitemap-webpack-plugin'
Quickstart
// webpack.config.js
const SitemapPlugin = require('sitemap-webpack-plugin');
module.exports = {
plugins: [
new SitemapPlugin({
base: 'https://example.com',
paths: [
'/page1',
'/page2',
{ path: '/page3', lastmod: '2023-01-01', priority: 0.8 },
],
options: {
filename: 'sitemap.xml',
skipgzip: false,
lastmod: true,
priority: 0.5,
changefreq: 'monthly',
},
}),
],
};