Skip to content

About

Leaflet for node.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Leaflet-node

npm version CI Node.js Version License: MIT

Leaflet-node brings the full Leaflet map API to Node.js so you can render maps without a browser.
Reuse the same map setup for server-side image generation, automated tests, and CI pipelines.

Installation

Requirements

  • Node.js 22.22+, 24.15+ or 26+ (the same range as jsdom)
  • Linux users need glibc >= 2.18 for the bundled @napi-rs/canvas
  • Peer dependency: leaflet ^1.9.0

Package managers

npm install leaflet-node leaflet
yarn add leaflet-node leaflet
pnpm add leaflet-node leaflet
bun add leaflet-node leaflet

Quick example

import L from 'leaflet-node';

const container = document.createElement('div');
container.style.width = '600px';
container.style.height = '400px';

const map = L.map(container);
map.setView([51.505538, -0.090005], 13);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '© OpenStreetMap contributors',
}).addTo(map);

map.setSize(600, 400);
await new Promise((resolve) => setTimeout(resolve, 1000));
await map.saveImage('map.png');

API

Importing leaflet-node sets up a jsdom window, a native canvas and Leaflet itself, then returns the normal Leaflet namespace. Everything you know from Leaflet works as usual; leaflet-node adds:

API What it does
map.setSize(width, height) Sets the map's pixel size (jsdom has no layout engine).
map.toBuffer(format?, quality?) Renders the current view to a PNG or JPEG Buffer. quality (0 to 1) applies to JPEG only.
map.saveImage(filename, { format?, quality? }) Renders the view and writes it to disk. Resolves with the filename.
initializeEnvironment(options) Changes the defaults for maps created afterwards: mapSize (default 1024×1024), enableAnimations (default false), userAgent (the jsdom navigator.userAgent), fontAssetBasePath.
setFontAssetBasePath(path) Points the fallback font loader at a custom font file or directory.
import L, { initializeEnvironment } from 'leaflet-node';

initializeEnvironment({ mapSize: { width: 512, height: 512 } });
const map = L.map(document.createElement('div')); // 512×512 unless you call setSize

leaflet-node/testing adds createTestMap, cleanupTestMaps, waitForTiles and waitForMapReady (see Tile loading helpers).

Testing with Jest

Leaflet-node can run inside Jest with either the default jsdom environment for API-only tests or the node environment for full canvas rendering.

API-only tests (no rendering)

Use the default environment and mock Leaflet to avoid creating a native canvas:

// tests/setup.ts
import { jest } from '@jest/globals';
const L = require('leaflet-node');

jest.doMock('leaflet', () => L);

Rendering tests (with canvas output)

Switch to the node environment so leaflet-node can provide its own jsdom instance and native canvas bindings:

/**
 * @jest-environment node
 */
import L from 'leaflet-node';

test('render map', async () => {
  const container = document.createElement('div');
  const map = L.map(container);

  map.setView([51.505538, -0.090005], 13);
  map.setSize(512, 512);

  await map.saveImage('map.png');
});

Known Issues

Tile Loading Warnings:

You may see warnings about "dynamic import callback" or "experimental-vm-modules" when running tests with tile layers in Jest. This is a Jest limitation with VM modules and doesn't affect the actual rendering—tiles that load successfully are rendered correctly.

To suppress these warnings, you can:

  1. Run Jest with experimental VM modules support:
NODE_OPTIONS='--experimental-vm-modules' jest
  1. Or, with ts-jest, enable isolated modules:
// jest.config.js
export default {
  testEnvironment: 'node',
  transform: {
    '^.+\\.tsx?$': ['ts-jest', { isolatedModules: true }],
  },
};

Tip

Need a ready-to-use map in tests? leaflet-node/testing exposes createTestMap, cleanupTestMaps, waitForTiles, and waitForMapReady helpers that work in both Vitest and Jest.

import { createTestMap, waitForMapReady } from 'leaflet-node/testing';

const map = createTestMap({ width: 400, height: 300 });
await waitForMapReady(map);

Exporting images

Maps can be exported to disk or kept in-memory:

// Save to disk with optional encoder options
await map.saveImage('map.jpeg', { format: 'jpeg', quality: 0.85 });

// Get a Buffer for further processing
const pngBuffer = await map.toBuffer('png');

If you need full control of the canvas, use map.toBuffer() and write the result yourself:

import { promises as fs } from 'fs';

const buffer = await map.toBuffer('png');
await fs.writeFile('map.png', buffer);

Tile loading helpers

Wait for tiles (with optional progress callbacks) before exporting:

import { waitForTiles } from 'leaflet-node/testing';

const layer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
layer.addTo(map);

await waitForTiles(layer, {
  timeout: 60_000,
  onProgress: ({ loaded, total }) => {
    console.log(`Loaded ${loaded}/${total} tiles`);
  },
});

Or wait for every tile layer on a map at once:

import { waitForMapReady } from 'leaflet-node/testing';

await waitForMapReady(map, {
  onTileProgress: (layer, progress) => {
    console.log(layer.getAttribution(), progress);
  },
});

📚 View the full documentation and live examples at jburnhams.github.io/leaflet-node.

Configuring fallback fonts

Leaflet-node automatically attempts to register the bundled Noto Sans fallback fonts. In environments where document.currentScript is unavailable (for example, Node.js test runners without a DOM shim), you can provide an explicit path to the font assets to avoid warning messages:

process.env.LEAFLET_NODE_FONT_BASE_PATH = '/absolute/path/to/NotoSans-Regular.ttf';
const L = await import('leaflet-node');

Alternatively, you can set the base path programmatically after importing the package:

import L, { setFontAssetBasePath } from 'leaflet-node';

setFontAssetBasePath('/absolute/path/to/NotoSans-Regular.ttf');

The base path can point directly to a font file or to a directory containing the bundled NotoSans-Regular.ttf asset.

Network requests

Tiles and remote images are fetched with undici. Each request:

  • sends User-Agent: leaflet-node (+https://github.com/jburnhams/leaflet-node). Public tile servers such as OpenStreetMap's require an identifying User-Agent; check your provider's usage policy before rendering at volume.
  • times out after 30 seconds, which fires the image's error event (and Leaflet's tileerror).

Proxy configuration

Leaflet-node honours the standard proxy environment variables:

export HTTP_PROXY=http://proxy.example.com:8080   # http:// URLs
export HTTPS_PROXY=http://proxy.example.com:8080  # https:// URLs (falls back to HTTP_PROXY)
export ALL_PROXY=http://proxy.example.com:8080    # used when neither of the above is set
export NO_PROXY=localhost,.internal.example.com   # hosts that bypass the proxy

Lower-case names work too. Changes are picked up on the next request.

Performance tips

  • Reuse map instances – keep a single map around and update the view instead of creating a new L.map() for every render.
  • Batch exports – kick off multiple independent exports with Promise.all() when your server has headroom.
  • Manage memory – always call map.remove() and drop references once you are done with a map so Node.js can reclaim resources.

Development

npm ci
npm run lint          # Biome lint + format check, then oxlint's type-aware rules
npm run format        # Biome: apply formatting
npm run typecheck     # TypeScript 7
npm run test:coverage # builds with tsdown, then runs Vitest with coverage
npm run test:docs     # builds the docs site and checks the rendered examples

tests/jest-fixture runs the built package under jest-environment-jsdom to make sure it keeps working inside consumers' Jest suites.


Questions? Open an issue on GitHub.

About

Leaflet for node.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages