Browser-native countries, states, and cities data with jsDelivr CDN and lazy loading. Same API as the server package β works in React, Vue, Svelte, Vite, and any browser environment.
Environment: π Browser-native (React, Vue, Svelte, Vite, and any browser environment)
- π Complete Data: 250+ countries, 5,000+ states, 150,000+ cities
- π Browser-Native: Runs directly in any browser via fetch and jsDelivr CDN
- π Minimal Bundle: <10KB initial load, lazy-load everything else
- π Lazy Loading: On-demand fetching β load only what you need
- π Translations: 18+ languages supported
- β° Timezone Data: Full timezone information per location
- π TypeScript: Full type definitions included
- π§ Configurable: Optional CDN override and cache tuning
npm install @countrystatecity/countries-browser
# or
yarn add @countrystatecity/countries-browser
# or
pnpm add @countrystatecity/countries-browserimport { getCountries, getStatesOfCountry, getCitiesOfState } from '@countrystatecity/countries-browser';
// Load all countries (~30KB gzipped)
const countries = await getCountries();
console.log(countries[0]);
// { id: 1, name: "United States", iso2: "US", emoji: "πΊπΈ", ... }
// Load states for a country (on-demand)
const states = await getStatesOfCountry('US');
console.log(states[0]);
// { id: 1, name: "California", iso2: "CA", ... }
// Load cities for a state (on-demand)
const cities = await getCitiesOfState('US', 'CA');
console.log(cities[0]);
// { id: 1, name: "Los Angeles", latitude: "34.05", ... }Get lightweight list of all countries (basic info only).
- Returns:
Promise<ICountry[]>
Get full country metadata including timezones and translations.
- Parameters:
code- ISO2 code (e.g., 'US') - Returns:
Promise<ICountryMeta | null>
Get all states/provinces for a country.
- Parameters:
countryCode- ISO2 code - Returns:
Promise<IState[]>
Get specific state details.
- Parameters:
countryCode,stateCode - Returns:
Promise<IState | null>
Get all cities in a specific state.
- Parameters:
countryCode,stateCode - Returns:
Promise<ICity[]>
Get a specific city by its numeric ID.
- Parameters:
countryCode,stateCode,cityId - Returns:
Promise<ICity | null>
Get ALL cities in an entire country.
- Warning: Large data size, use sparingly
- Returns:
Promise<ICity[]>
Get every city globally.
- Warning: MASSIVE data (8MB+), rarely needed
- Returns:
Promise<ICity[]>
Check if country code exists.
- Returns:
Promise<boolean>
Check if state code exists in a country.
- Returns:
Promise<boolean>
Search cities by partial name match.
- Returns:
Promise<ICity[]>
Get country name from code.
- Returns:
Promise<string | null>
Get state name from code.
- Returns:
Promise<string | null>
Get timezone for specific city.
- Returns:
Promise<string | null>
Get all timezones for a country.
- Returns:
Promise<string[]>
import { configure, resetConfiguration, clearCache } from '@countrystatecity/countries-browser';
// Self-host data instead of jsDelivr
configure({
baseURL: 'https://my-cdn.com/data',
timeout: 10000,
cacheSize: 100,
});
// Reset to defaults
resetConfiguration();
// Clear in-memory cache
clearCache();import { useState, useEffect } from 'react';
import { getCountries, getStatesOfCountry, getCitiesOfState } from '@countrystatecity/countries-browser';
import type { ICountry, IState, ICity } from '@countrystatecity/countries-browser';
export function LocationSelector() {
const [countries, setCountries] = useState<ICountry[]>([]);
const [states, setStates] = useState<IState[]>([]);
const [cities, setCities] = useState<ICity[]>([]);
const [selectedCountry, setSelectedCountry] = useState('');
const [selectedState, setSelectedState] = useState('');
useEffect(() => {
getCountries().then(setCountries);
}, []);
useEffect(() => {
if (!selectedCountry) { setStates([]); return; }
getStatesOfCountry(selectedCountry).then(setStates);
}, [selectedCountry]);
useEffect(() => {
if (!selectedCountry || !selectedState) { setCities([]); return; }
getCitiesOfState(selectedCountry, selectedState).then(setCities);
}, [selectedCountry, selectedState]);
return (
<div>
<select value={selectedCountry} onChange={(e) => { setSelectedCountry(e.target.value); setSelectedState(''); }}>
<option value="">Select Country</option>
{countries.map(c => <option key={c.iso2} value={c.iso2}>{c.name}</option>)}
</select>
<select value={selectedState} onChange={(e) => setSelectedState(e.target.value)} disabled={!selectedCountry}>
<option value="">Select State</option>
{states.map(s => <option key={s.iso2} value={s.iso2}>{s.name}</option>)}
</select>
<select disabled={!selectedState}>
<option value="">Select City</option>
{cities.map(c => <option key={c.id} value={c.id}>{c.name}</option>)}
</select>
</div>
);
}import { getCountries, NetworkError } from '@countrystatecity/countries-browser';
try {
const countries = await getCountries();
} catch (error) {
if (error instanceof NetworkError) {
console.error(`CDN request failed: ${error.statusCode} at ${error.url}`);
}
}Invalid codes return null or [] gracefully β no exceptions thrown.
import type { ICountry, ICountryMeta, IState, ICity, ITimezone } from '@countrystatecity/countries-browser';| Action | Bundle Size |
|---|---|
| Install & import | ~5KB |
| Load countries | ~30KB gzipped |
| Load US states | ~30KB |
| Load CA cities | ~15KB |
| Typical usage | ~50KB |
@countrystatecity/countries (server) |
@countrystatecity/countries-browser |
|
|---|---|---|
| Environment | Node.js only | Browser + Node.js |
| Data loading | fs.readFileSync |
fetch() from CDN |
| Initial bundle | ~5KB | ~5KB |
| Configuration | None needed | Optional CDN override |
| Lazy loading | Yes | Yes |
# Run all tests
npm test
# Watch mode
npm run test:watchAll packages include comprehensive tests:
- β Unit tests
- β Integration tests
- β iOS/Safari compatibility tests
Every push and PR automatically:
- β Runs type checking
- β Executes comprehensive tests
- β Builds the package
- β Validates bundle sizes
- β Tests iOS/Safari compatibility
Automated publishing to NPM on version changes:
- π Detects version bumps in package.json
- π¦ Builds and tests before publishing
- π Publishes to NPM registry
- π·οΈ Creates GitHub release with changelog
This package and its data are licensed under the Open Database License (ODbL) v1.0. The data is sourced from the Countries States Cities Database which is also licensed under ODbL-1.0.
You are free to share, create, and adapt this database as long as you attribute the original sources, distribute adaptations under the same license, and don't use technical restrictions to lock down the data.
Contributions are welcome! Please open an issue or PR.
For data-related issues (incorrect country names, missing cities, wrong coordinates, etc.), please report them to the Countries States Cities Database repository, which is the source of data for this package.
This package is part of the @countrystatecity package ecosystem:
-
@countrystatecity/countries β Server-side countries, states, and cities database. Environment: Node.js, Next.js API routes, Express. Bundle: <10KB initial load.
-
@countrystatecity/countries-browser (This package) β Browser-native version with jsDelivr CDN and lazy loading. Environment: React, Vue, Svelte, Vite, any browser. Same API as the server package β zero config, just import and use.
-
@countrystatecity/timezones β Comprehensive timezone data with conversion utilities. Environment: Server-side only. Bundle: <20KB initial load.