Skip to content

dr5hn/countrystatecity-countries

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

@countrystatecity/countries

npm CI npm downloads

Official countries, states, and cities database with iOS/Safari support and minimal bundle size.

Environment: πŸ–₯️ Server-side only (Node.js, Next.js API routes, Express, etc.)

✨ Features

  • 🌍 Complete Data: 250+ countries, 5,000+ states, 150,000+ cities
  • πŸ“± iOS Compatible: No stack overflow errors on Safari/iOS browsers
  • πŸš€ Minimal Bundle: <10KB initial load, lazy-load everything else
  • πŸ”„ Lazy Loading: Dynamic imports for optimal performance
  • 🌐 Translations: 18+ languages supported
  • ⏰ Timezone Data: Full timezone information per location
  • πŸ“ TypeScript: Full type definitions included
  • πŸ”§ Tree-Shakeable: Only bundle what you use

πŸ“¦ Installation

npm install @countrystatecity/countries
# or
yarn add @countrystatecity/countries
# or
pnpm add @countrystatecity/countries

⚠️ Server-Side Only: This package requires Node.js file system access and cannot be used in browser environments. For frontend use, create API endpoints using this package and fetch data from your client.

πŸš€ Quick Start

import { getCountries, getStatesOfCountry, getCitiesOfState } from '@countrystatecity/countries';

// Get all countries (lightweight - ~5KB)
const countries = await getCountries();
console.log(countries[0]);
// { id: 1, name: "United States", iso2: "US", emoji: "πŸ‡ΊπŸ‡Έ", ... }

// Get all states in a country (~10-100KB depending on country)
const states = await getStatesOfCountry('US');
console.log(states[0]);
// { id: 1, name: "California", iso2: "CA", ... }

// Get all cities in a state (~5-200KB depending on state)
const cities = await getCitiesOfState('US', 'CA');
console.log(cities[0]);
// { id: 1, name: "Los Angeles", latitude: "34.05", ... }

πŸ“– API Reference

Core Functions

getCountries()

Get lightweight list of all countries (basic info only).

  • Returns: Promise<ICountry[]>
  • Bundle Impact: ~5KB

getCountryByCode(countryCode: string)

Get full country metadata including timezones and translations.

  • Parameters: countryCode - ISO2 code (e.g., 'US')
  • Returns: Promise<ICountryMeta | null>
  • Bundle Impact: ~5KB per country

getStatesOfCountry(countryCode: string)

Get all states/provinces for a country.

  • Parameters: countryCode - ISO2 code
  • Returns: Promise<IState[]>
  • Bundle Impact: ~10-100KB depending on country

getStateByCode(countryCode: string, stateCode: string)

Get specific state details.

  • Parameters: countryCode, stateCode
  • Returns: Promise<IState | null>

getCitiesOfState(countryCode: string, stateCode: string)

Get all cities in a specific state.

  • Parameters: countryCode, stateCode
  • Returns: Promise<ICity[]>
  • Bundle Impact: ~5-200KB depending on state

getAllCitiesOfCountry(countryCode: string)

Get ALL cities in an entire country.

  • Warning: Large data size, use sparingly
  • Returns: Promise<ICity[]>

getAllCitiesInWorld()

Get every city globally.

  • Warning: MASSIVE data (8MB+), rarely needed
  • Returns: Promise<ICity[]>

Utility Functions

isValidCountryCode(countryCode: string)

Check if country code exists.

  • Returns: Promise<boolean>

isValidStateCode(countryCode: string, stateCode: string)

Check if state code exists in a country.

  • Returns: Promise<boolean>

searchCitiesByName(countryCode: string, stateCode: string, searchTerm: string)

Search cities by partial name match.

  • Returns: Promise<ICity[]>

getCountryNameByCode(countryCode: string)

Get country name from code.

  • Returns: Promise<string | null>

getStateNameByCode(countryCode: string, stateCode: string)

Get state name from code.

  • Returns: Promise<string | null>

getTimezoneForCity(countryCode: string, stateCode: string, cityName: string)

Get timezone for specific city.

  • Returns: Promise<string | null>

getCountryTimezones(countryCode: string)

Get all timezones for a country.

  • Returns: Promise<string[]>

πŸ”§ TypeScript Types

import type { ICountry, ICountryMeta, IState, ICity, ITimezone } from '@countrystatecity/countries';

🎯 Why This Package?

The Problem

The popular country-state-city package (162K weekly downloads) has critical issues:

  • πŸ”΄ 8MB bundle size (includes ALL data)
  • πŸ”΄ iOS Safari crashes with stack overflow errors
  • πŸ”΄ Unmaintained for 2+ years
  • πŸ”΄ Static imports force entire bundle

Our Solution

  • βœ… Minimal bundle (<10KB initial)
  • βœ… Dynamic imports & lazy loading
  • βœ… iOS/Safari compatible
  • βœ… Always updated from authoritative database
  • βœ… Tree-shakeable & code-splittable

πŸ“Š Bundle Size Comparison

Action @countrystatecity/countries country-state-city
Install & import 5KB 8MB
Load countries +2KB -
Load US states +30KB -
Load CA cities +15KB -
Total for typical use ~50KB 8MB

160x smaller bundle size!

πŸ§ͺ Testing

# Run all tests
npm test

# Run iOS compatibility tests specifically
npm run test:ios

# Watch mode
npm run test:watch

All packages include comprehensive tests:

  • βœ… Unit tests
  • βœ… Integration tests
  • βœ… iOS/Safari compatibility tests

πŸ”„ CI/CD & Automation

Continuous Integration

Every push and PR automatically:

  • βœ… Runs type checking
  • βœ… Executes comprehensive tests
  • βœ… Builds the package
  • βœ… Validates bundle sizes
  • βœ… Tests iOS/Safari compatibility

Automated Publishing

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

πŸ“„ License

ODbL-1.0 Β© dr5hn

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: Credit the original sources
  • Share-Alike: Distribute adaptations under the same license
  • Keep Open: Don't use technical restrictions

🀝 Contributing

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.

πŸ“¦ Package Ecosystem

This package is part of the @countrystatecity package ecosystem:

Available Packages

  • @countrystatecity/countries (This package)

    • Server-side countries, states, and cities database
    • Environment: Node.js, Next.js API routes, Express
    • Bundle: <10KB initial load
  • @countrystatecity/timezones

    • Comprehensive timezone data with conversion utilities
    • Environment: Server-side only
    • Bundle: <20KB initial load

Environment Guide

Use this package (@countrystatecity/countries) when:

  • βœ… Building API endpoints or backends
  • βœ… Using Next.js App Router server components
  • βœ… Running in Node.js, serverless functions (Vercel, AWS Lambda)
  • βœ… You have file system access
  • βœ… Building command-line tools

For browser/frontend use:

Until the browser package is available, create API endpoints:

// pages/api/countries.ts (Next.js API route)
import { getCountries } from '@countrystatecity/countries';

export default async function handler(req, res) {
  const countries = await getCountries();
  res.json(countries);
}

// components/CountrySelector.tsx (Frontend)
const countries = await fetch('/api/countries').then(r => r.json());

πŸ”— Links

About

Official Country State City - NPM Packages

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published