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 v6.2.0

Sitemap-generating lib/cli

sitemap MIT LicenseBuild StatusMonthly Downloads

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


Table of Contents


npm install --save sitemap

Generate a one time sitemap from a list of urls

If you are just looking to take a giant list of URLs and turn it into some sitemaps, try out our CLI. The cli can also parse, update and validate existing sitemaps.

npx sitemap < listofurls.txt # `npx sitemap -h` for more examples and a list of options.

For programmatic one time generation of a sitemap try:

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

  // An array with your links
  const links = [{ url: '/page-1/',  changefreq: 'daily', priority: 0.3  }]

  // Create a stream to write to
  const stream = new SitemapStream( { hostname: 'https://...' } )

  // Loop over your links and add them to your stream
  links.forEach( link => stream.write( link ) )

  // End the stream

  // Return a promise that resolves with your XML string
  return streamToPromise( stream ).then( data => data.toString() )

Serve a sitemap from a server and periodically update it

Use this if you have less than 50 thousand urls. See SitemapAndIndexStream for if you have more.

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())

    // pipe your entries or directly write them.
    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)
    // make sure to attach a write stream such as streamToPromise before ending
    // stream write the response
    pipeline.pipe(res).on('error', (e) => {throw e})
  } catch (e) {

app.listen(3000, () => {

Create sitemap and index files from one large list

If you know you are definitely going to have more than 50,000 urls in your sitemap, you can use this slightly more complex interface to create a new sitemap every 45,000 entries and add that file to a sitemap index.

const { createReadStream, createWriteStream } = require('fs');
const { resolve } = require('path');
const { createGzip } = require('zlib')
const {
} = require('sitemap')

// writes sitemaps and index out to the destination you provide
  hostname: '',
  destinationDir: './',
  sourceData: lineSeparatedURLsToSitemapOptions(
  // or
  sourceData: [{ url: '/page-1/', changefreq: 'daily'}, ...],
  // or
  sourceData: './your-data.json.txt',
}).then(() => {
  // Do follow up actions

Want to customize that?

const { createReadStream, createWriteStream } = require('fs');
const { resolve } = require('path');
const { createGzip } = require('zlib')
const {
} = require('sitemap')

const sms = new SitemapAndIndexStream({
  limit: 50000, // defaults to 45k
  // SitemapAndIndexStream will call this user provided function every time
  // it needs to create a new sitemap file. You merely need to return a stream
  // for it to write the sitemap urls to and the expected url where that sitemap will be hosted
  getSitemapStream: (i) => {
    const sitemapStream = new SitemapStream({ hostname: '' });
    const path = `./sitemap-${i}.xml`;

      .pipe(createGzip()) // compress the output of the sitemap
      .pipe(createWriteStream(resolve(path + '.gz'))); // write it to sitemap-NUMBER.xml

    return [new URL(path, '').toString(), sitemapStream];

// when reading from a file

// or reading straight from an in-memory array

const arrayOfSitemapItems = [{ url: '/page-1/', changefreq: 'daily'}, ...]
arrayOfSitemapItems.forEach(item => sms.write(item))

Options you can pass

const { SitemapStream, streamToPromise } = require('sitemap');
const smStream = new SitemapStream({
  hostname: '',
  xslUrl: "",
  lastmodDateOnly: false, // print date not time
  xmlns: { // trim the xml namespace
    news: true, // flip to false to omit the xml namespace for news
    xhtml: true,
    image: true,
    video: true,
    custom: [
// coalesce stream to value
// alternatively you can pipe to another stream

  url: '/page1',
  changefreq: 'weekly',
  priority: 0.8, // A hint to the crawler that it should prioritize this over items less than 0.8

// each sitemap entry supports many options
// See [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',
      'player_loc:allow_embed': 'yes'
  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


For more examples see the examples directory


Full API docs can be found here


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