Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including sitemap with all npm packages installed. Try it out:

var sitemap = require("sitemap")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

sitemap v5.1.0

Sitemap-generating lib/cli

sitemap.js Build Status

sitemap.js is a high-level sitemap-generating library/CLI that makes creating sitemap XML files easy.


Table of Contents


npm install --save sitemap



Just feed the list of urls into sitemap

npx sitemap < listofurls.txt

Or validate an existing sitemap (requires libxml)

npx sitemap --validate sitemap.xml

Or take an existing sitemap and turn it into options that can be fed into the libary

npx sitemap --parse sitemap.xml

Or prepend some new urls to an existing sitemap

npx sitemap --prepend sitemap.xml < listofurls.json # or txt

As a library

const { SitemapStream, streamToPromise } = require('../dist/index')
// Creates a sitemap object given the input configuration with URLs
const sitemap = new SitemapStream({ hostname: '' });
sitemap.write({ url: '/page-1/', changefreq: 'daily', priority: 0.3 })

  .then(sm => console.log(sm.toString()))

Resolves to a string containing the XML data

 <?xml version="1.0" encoding="UTF-8"?><urlset xmlns="" xmlns:news="" xmlns:xhtml="" xmlns:image="" xmlns:video=""><url><loc></loc><changefreq>daily</changefreq><priority>0.3</priority></url><url><loc></loc></url></urlset>

Example of using sitemap.js with express

const express = require('express')
const { SitemapStream, streamToPromise } = require('sitemap')
const { createGzip } = require('zlib')

const app = express()
let sitemap

app.get('/sitemap.xml', function(req, res) {
  res.header('Content-Type', 'application/xml');
  res.header('Content-Encoding', 'gzip');
  // if we have a cached entry send it
  if (sitemap) {
  try {
    const smStream = new SitemapStream({ hostname: '' })
    const pipeline = smStream.pipe(createGzip())

    smStream.write({ url: '/page-1/',  changefreq: 'daily', priority: 0.3 })
    smStream.write({ url: '/page-2/',  changefreq: 'monthly',  priority: 0.7 })
    smStream.write({ url: '/page-3/'})    // changefreq: 'weekly',  priority: 0.5
    smStream.write({ url: '/page-4/',   img: "" })

    // cache the response
    streamToPromise(pipeline).then(sm => sitemap = sm)
    // stream the response
    pipeline.pipe(res).on('error', (e) => {throw e})
  } catch (e) {

app.listen(3000, () => {

Stream writing a sitemap

The sitemap stream is around 20% faster and only uses ~10% the memory of the traditional interface

const fs = require('fs');
const { SitemapStream } = require('sitemap')
// external libs provided as example only
const { parser } = require('stream-json/Parser');
const { streamArray } = require('stream-json/streamers/StreamArray');
const { streamValues } = require('stream-json/streamers/StreamValues');
const map = require('through2-map')
const { createGzip } = require('zlib')

// parsing line separated json or JSONStream
const pipeline = fs
  .pipe(map.obj(chunk => chunk.value))
  // SitemapStream does the heavy lifting
  // You must provide it with an object stream
  .pipe(new SitemapStream());

// parsing JSON file
const pipeline = fs
  .pipe(map.obj(chunk => chunk.value))
  // SitemapStream does the heavy lifting
  // You must provide it with an object stream
  .pipe(new SitemapStream({ hostname: '' }))

// coalesce into value for caching
  let cachedXML
    .pipe(map.obj(chunk => chunk.value))
    .pipe(new SitemapStream({ hostname: '' }))
  ).then(xmlBuffer => cachedXML = xmlBuffer)

Example of most of the options you can use for sitemap

const { SitemapStream, streamToPromise } = require('sitemap');
const smStream = new SitemapStream({ hostname: '' })
// coalesce stream to value
// alternatively you can pipe to another stream

  url: '/page1',
  changefreq: 'weekly',
  priority: 0.8,
  lastmodfile: 'app/assets/page1.html'

  url: '/page2',
  changefreq: 'weekly',
  priority: 0.8,
  /* useful to monitor template content files instead of generated static files */
  lastmodfile: 'app/templates/page2.hbs'

// each sitemap entry supports many options
// See [Sitemap Item Options](#sitemap-item-options) below for details
  url: '',
  img: [
      url: '',
      caption: 'An image',
      title: 'The Title of Image One',
      geoLocation: 'London, United Kingdom',
      license: ''
      url: '',
      caption: 'Another image',
      title: 'The Title of Image Two',
      geoLocation: 'London, United Kingdom',
      license: ''
  video: [
      thumbnail_loc: '',
      title: 'A video title',
      description: 'This is a video'
      thumbnail_loc: '',
      title: 'A video with an attribute',
      description: 'This is another video',
      'player_loc': '',
      'player_loc:autoplay': 'ap=1'
  links: [
    { lang: 'en', url: '' },
    { lang: 'ja', url: '' }
  androidLink: 'android-app://',
  news: {
    publication: {
      name: 'The Example Times',
      language: 'en'
    genres: 'PressRelease, Blog',
    publication_date: '2008-12-23',
    title: 'Companies A, B in Merger Talks',
    keywords: 'business, merger, acquisition, A, B',
    stock_tickers: 'NASDAQ:A, NASDAQ:B'
// indicate there is nothing left to write

Building just the sitemap index file

The sitemap index file merely points to other sitemaps

const { buildSitemapIndex } = require('sitemap')
const smi = buildSitemapIndex({
  urls: ['', ''],
  xslUrl: '' // optional

Auto creating sitemap and index files from one large list

const { createSitemapsAndIndex } = require('sitemap')
const smi = createSitemapsAndIndex({
  hostname: '',
  sitemapName: 'sm-test',
  sitemapSize: 1,
  targetFolder: require('os').tmpdir(),
  urls: ['', '']


Sitemap - deprecated

const { Sitemap } = require('sitemap')
const sm = new Sitemap({
    urls: [{ url: '/path' }],
    hostname: '',
    cacheTime: 0, // default
    level: 'warn', // default warns if it encounters bad data
    lastmodDateOnly: false // relevant for baidu
sm.toString() // returns the xml as a string



Converts the urls stored in an instance of Sitemap to a valid sitemap xml document as a string. Accepts a boolean as its first argument to designate on whether to pretty print. Defaults to false.


alias for toString


sm.toGzip ((xmlGzippedBuffer) => console.log(xmlGzippedBuffer))

Like toString, it builds the xmlDocument, then it runs gzip on the resulting string and returns it as a Buffer via callback or direct invocation



Cache will be emptied and will be bypassed until set again



Returns true if it has been less than cacheTimeout ms since cache was set



Stores the passed in string on the instance to be used when toString is called within the configured cacheTimeout returns the passed in string unaltered


sm.add('/path', 'warn')

Adds the provided url to the sitemap instance takes an optional parameter level for whether to print a console warning in the event of bad data 'warn' (default), throw an exception 'throw', or quietly ignore bad data 'silent' returns the number of locations currently in the sitemap instance



Returns true if path is already a part of the sitemap instance, false otherwise.



Removes the provided url or url option from the sitemap instance


Sitemap.normalizeURL('/', '', false)

Static function that returns the stricter form of a options passed to SitemapItem. The third argument is whether to use date-only varient of lastmod. For baidu.


Sitemap.normalizeURLs(['', {url: '/'}], '', false)

Static function that takes an array of urls and returns a Map of their resolved url to the strict form of SitemapItemOptions


Build a sitemap index file

const { buildSitemapIndex } = require('sitemap')
const index = buildSitemapIndex({
  urls: [{ url: '', lastmod: '2019-07-01' }, ''],
  lastmod: '2019-07-29'


Create several sitemaps and an index automatically from a list of urls

const { createSitemapsAndIndex } = require('sitemap')
  urls: [/* list of urls */],
  targetFolder: 'absolute path to target folder',
  hostname: '',
  cacheTime: 600,
  sitemapName: 'sitemap',
  sitemapSize: 50000, // number of urls to allow in each sitemap
  gzip: true, // whether to gzip the files


Resolve or reject depending on whether the passed in xml is a valid sitemap. This is just a wrapper around the xmlLint command line tool and thus requires xmlLint.

const { createReadStream } = require('fs')
const { xmlLint } = require('sitemap')
  () => console.log('xml is valid'),
  ([err, stderr]) => console.error('xml is invalid', stderr)


Read xml and resolve with the configuration that would produce it or reject with an error

const { createReadStream } = require('fs')
const { parseSitemap, createSitemap } = require('sitemap')
  // produces the same xml
  // you can, of course, more practically modify it or store it
  (xmlConfig) => console.log(createSitemap(xmlConfig).toString()),
  (err) => console.log(err)


A Transform for turning a Readable stream of either SitemapItemOptions or url strings into a Sitemap. The readable stream it transforms must be in object mode.

const { SitemapStream } = require('sitemap')
const sms = new SitemapStream({
  hostname: '', // optional only necessary if your paths are relative
  lastmodDateOnly: false // defaults to false, flip to true for baidu
const readable = // a readable stream of objects


Takes a stream of xml and transforms it into a stream of ISitemapOptions. Use this to parse existing sitemaps into config options compatible with this library

const { createReadStream, createWriteStream } = require('fs');
const { XMLToISitemapOptions, ObjectStreamToJSON } = require('sitemap');

// turn the xml into sitemap option item options
.pipe(new XMLToISitemapOptions())
// convert the object stream to JSON
.pipe(new ObjectStreamToJSON())
// write the library compatible options to disk


Takes a stream of urls or sitemapoptions likely from fs.createReadStream('./path') and returns an object stream of sitemap items.


Takes a stream returns a promise that resolves when stream emits finish.

const { streamToPromise, SitemapStream } = require('sitemap')
const sitemap = new SitemapStream({ hostname: '' });
sitemap.write({ url: '/page-1/', changefreq: 'daily', priority: 0.3 })
streamToPromise(sitemap).then(buffer => console.log(buffer.toString())) // emits the full sitemap


A Transform that converts a stream of objects into a JSON Array or a line separated stringified JSON.

  • @param [lineSeparated=false] whether to separate entries by a new line or comma
const stream = Readable.from([{a: 'b'}])
  .pipe(new ObjectStreamToJSON())
// prints {"a":"b"}

Sitemap Item Options

urlstring only required property for every sitemap entry
lastmodstring'2019-07-29' or '2019-07-22T05:58:37.037Z'When the page we as last modified use the W3C Datetime ISO8601 subset
changefreqstring'weekly'How frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page. Please note that the value of this tag is considered a hint and not a command. See for the acceptable values
prioritynumber0.6The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0. This value does not affect how your pages are compared to pages on other sites—it only lets the search engines know which pages you deem most important for the crawlers. The default priority of a page is 0.5.
imgobject[]see #ISitemapImage
videoobject[]see #IVideoItem
linksobject[]see #ILinkItemTell search engines about localized versions
newsobjectsee #INewsItem
cdatabooleantruewrap url in cdata xml escape


Sitemap image

urlstring URL of the image.
captionstring - optional'Here we did the stuff'The caption of the image.
titlestring - optional'Star Wars EP IV'The title of the image.
geoLocationstring - optional'Limerick, Ireland'The geographic location of the image.
licensestring - optional URL to the license of the image.


Sitemap video.

thumbnail_locstring""A URL pointing to the video thumbnail image file
titlestring'2018:E6 - GoldenEye: Source'The title of the video.
descriptionstring'We play gun game in GoldenEye: Source with a good friend of ours. His name is Gruchy. Dan Gruchy.'A description of the video. Maximum 2048 characters.
content_locstring - optional""A URL pointing to the actual video media file. Should be one of the supported formats.HTML is not a supported format. Flash is allowed, but no longer supported on most mobile platforms, and so may be indexed less well. Must not be the same as the <loc> URL.
player_locstring - optional""A URL pointing to a player for a specific video. Usually this is the information in the src element of an <embed> tag. Must not be the same as the <loc> URL
'player_loc:autoplay'string - optional'ap=1'a string the search engine can append as a query param to enable automatic playback
durationnumber - optional600duration of video in seconds
expiration_datestring - optional"2012-07-16T19:20:30+08:00"The date after which the video will no longer be available
view_countstring - optional'21000000000'The number of times the video has been viewed.
publication_datestring - optional"2018-04-27T17:00:00.000Z"The date the video was first published, in W3C format.
categorystring - optional"Baking"A short description of the broad category that the video belongs to. This is a string no longer than 256 characters.
restrictionstring - optional"IE GB US CA"Whether to show or hide your video in search results from specific countries.
restriction:relationshipstring - optional"deny"
gallery_locstring - optional""Currently not used.
gallery_loc:titlestring - optional"awhu series page"Currently not used.
pricestring - optional"1.99"The price to download or view the video. Omit this tag for free videos.
price:resolutionstring - optional"HD"Specifies the resolution of the purchased version. Supported values are hd and sd.
price:currencystring - optional"USD"currency [Required] Specifies the currency in ISO 4217 format.
price:typestring - optional"rent"type [Optional] Specifies the purchase option. Supported values are rent and own.
uploaderstring - optional"GrillyMcGrillerson"The video uploader's name. Only one <video:uploader> is allowed per video. String value, max 255 characters.
platformstring - optional"tv"Whether to show or hide your video in search results on specified platform types. This is a list of space-delimited platform types. See for more detail
platform:relationshipstring 'Allow'|'Deny' - optional'Allow'
idstring - optional
tagstring[] - optional['Baking']An arbitrary string tag describing the video. Tags are generally very short descriptions of key concepts associated with a video or piece of content.
ratingnumber - optional2.5The rating of the video. Supported values are float numbers i
family_friendlystring 'YES'|'NO' - optional'YES'
requires_subscriptionstring 'YES'|'NO' - optional'YES'Indicates whether a subscription (either paid or free) is required to view the video. Allowed values are yes or no.
livestring 'YES'|'NO' - optional'NO'Indicates whether the video is a live stream. Supported values are yes or no.




accessstring - 'Registration' | 'Subscription''Registration' - optional
publicationobjectsee following options
publication['name']string'The Example Times'The <name> is the name of the news publication. It must exactly match the name as it appears on your articles on, except for anything in parentheses.
publication['language']string'en'he <language> is the language of your publication. Use an ISO 639 language code (2 or 3 letters).
genresstring - optional'PressRelease, Blog'
publication_datestring'2008-12-23'Article publication date in W3C format, using either the "complete date" (YYYY-MM-DD) format or the "complete date plus hours, minutes, and seconds"
titlestring'Companies A, B in Merger Talks'The title of the news article.
keywordsstring - optional"business, merger, acquisition, A, B"
stock_tickersstring - optional"NASDAQ:A, NASDAQ:B"


See LICENSE file.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free