๐บ๏ธ Optional Maps
Keep your bundle small. Add heavy country/region maps only when you actually need them.
By default, svg-world-maps includes only the World map to ensure the smallest possible initial bundle size. All other country and region maps are optional and can be added to your project on-demand with a single CLI command.
๐ We've added a new npx register-map command that can automatically scan your project and register your added maps, saving you from writing boilerplate import code!
๐ How to Add an Optional Mapโ
Let's use Afghanistan as an example. The process is the same for any optional map.
Step 1: Download the map via CLIโ
Run this command in your project root:
npx add-map afghanistan
โ This safely downloads the optimized map data into your project:
your-project/
โโโ src/
โ โโโ maps/
โ โโโ AF.ts <-- New map data file
Step 2: Register the map in your codeโ
Before you can render it, you must register the map data with the library.
import { registerMapData, createMap } from 'svg-world-maps';
import afghanistanData from './src/maps/AF'; // Adjust path to your folder structure
// 1. Register the data
registerMapData('afghanistan', afghanistanData);
// 2. Now you can create the map!
const map = createMap('afghanistan', {
background: '#f0f0f0',
borders: '#333',
tooltip: true,
size: 'lg'
});
Step 3: Render it in your frameworkโ
Once registered, use it exactly like the built-in world map:
- Vanilla JS
- React / Next.js
- Vue 3
- Svelte
document.getElementById('map-container').innerHTML = map;
<div dangerouslySetInnerHTML={{ __html: map }} />
<template>
<div v-html="map" />
</template>
{@html map}
โก Automated Registration (v0.7.0+)โ
Tired of manually importing and registering every map? We built a tool for that.
After adding maps via npx add-map, simply run:
npx register-map
This script will automatically:
- Scan your
src/maps/directory. - Generate a centralized registration file (e.g.,
src/maps/register.ts). - Provide you with a single import to initialize all your added maps at once.
Run npx register-map every time you add a new map during development to keep your setup completely frictionless.
๐ Registering Multiple Maps Manuallyโ
If you prefer manual control or are using an older version, you can register as many maps as you need:
import { registerMapData, createMap } from 'svg-world-maps';
import afData from './src/maps/AF';
import usData from './src/maps/US';
import deData from './src/maps/DE';
registerMapData('afghanistan', afData);
registerMapData('usa', usData);
registerMapData('germany', deData);
// Use them anywhere in your app
const afMap = createMap('afghanistan');
const usMap = createMap('usa', { tooltip: true });
โ ๏ธ Common Errors & Fixesโ
โ "Map type 'afghanistan' not found in registry"
Cause: You called createMap('afghanistan') before registering its data, or the registration failed silently.
Fix:
- Ensure you ran
npx add-map afghanistan. - Verify that
registerMapData('afghanistan', data)is executed before anycreateMap('afghanistan')call in your application lifecycle (e.g., at the top of your main entry file likemain.jsorApp.tsx).
โ "Cannot find module './src/maps/AF'"
Cause: The map file wasn't downloaded, or your import path is incorrect.
Fix:
- Run
npx add-map afghanistanagain to ensure the file exists. - Check your folder structure. If your maps folder is elsewhere, adjust the import:
// Using a relative pathimport afData from '../maps/AF';// Or using a TypeScript/Vite alias (if configured)import afData from '@/maps/AF';
โ "registerMapData is not a function"
Cause: You are using an outdated version of the library (pre-v0.3.0).
Fix: Update to the latest version:
npm install svg-world-maps@latest
# or
yarn upgrade svg-world-maps@latest
๐งน Removing an Optional Mapโ
Don't need a map anymore? Remove it to shrink your production bundle:
- Delete the map file:
rm src/maps/AF.ts
- Remove the registration code (or re-run
npx register-mapto auto-clean). - Rebuild your project to confirm the bundle size reduction.
๐ก Note: The
npx add-mapcommand is idempotent. Running it multiple times just re-downloads/overwrites the file safely. No complex cleanup is needed unless you actively want to remove the map.
๐ Available Optional Mapsโ
As of v0.7.0, we support 34+ country and region maps. Add any of these using npx add-map <name>:
| Region | CLI Command | Details |
|---|---|---|
| ๐ World | (Built-in) | โ Included by default (195 countries) |
| ๐ฆ๐ซ Afghanistan | npx add-map afghanistan | 34 provinces |
| ๐บ๐ธ USA | npx add-map usa | 51 states/regions |
| ๐ฉ๐ช Germany | npx add-map germany | 16 states |
| ๐ฎ๐ณ India | npx add-map india | 36 states/territories |
| ๐ฎ๐ท Iran | npx add-map iran | 31 provinces |
| ๐จ๐ฆ Canada | npx add-map canada | 13 provinces/territories |
| ๐ง๐ท Brazil | npx add-map brazil | 27 states |
| ๐ซ๐ท France | npx add-map france | 13 regions |
| ๐ฆ๐บ Australia | npx add-map australia | 8 states/territories |
| ๐ฌ๐ง Great Britain | npx add-map gb | 232 regions |
| ๐ช๐บ Europe | npx add-map europe | 45 countries |
| ๐ Africa | npx add-map africa | 50 countries |
| ...and 20+ more! | npx add-map <name> | Pakistan, Argentina, Austria, Denmark, Finland, Greenland, Iceland, Israel, Kuwait, Lebanon, Luxembourg, Netherlands, Norway, Oman, Poland, Singapore, Sweden, Switzerland, UAE, Vatican. |
We are constantly adding new maps! Open a GitHub Discussion or Issue to request your country or region.