Country States Database
The `country-states` package provides a lightweight, focused database containing country and state/subdivision data, primarily adhering to the ISO 3166-2 standard. It offers a programmatic way to access geographical subdivisions for various countries. As of version 0.0.10, it is in an early but functional state, likely receiving updates as data changes or expands, rather than on a strict release cadence. Its key differentiator lies in its simplicity and direct access to structured country-state information without complex APIs or extensive feature sets, making it suitable for applications requiring basic geographical data lookups where detailed geographic features are not needed.
Common errors
-
TypeError: states is not a function
cause Incorrect import or require statement, or trying to access `states` before the module is properly loaded.fixUse `import { states } from 'country-states';` for ESM or `const { states } = require('country-states');` for CJS. Verify the package is correctly installed via `npm install country-states`. -
console.log(states('XX')) returns undefined or an empty arraycause The country code 'XX' was not included in the array passed to the `compile` function, or the `compile` function was not called at all.fixBefore calling `states('XX')`, ensure you have executed `compile(['XX', ...])` with 'XX' explicitly listed in the array of country codes. -
Cannot find module 'country-states'
cause The package has not been installed or is not correctly resolved in your project's `node_modules`.fixRun `npm install country-states` or `yarn add country-states` in your project directory.
Warnings
- gotcha The `compile` function must be called with an array of ISO 3166-1 alpha-2 country codes before attempting to retrieve states for those countries. Calling `states()` for an uncompiled country will return `undefined` or an empty array.
- breaking As a package with a version number below 1.0.0, `country-states` does not guarantee API stability. Minor versions may introduce breaking changes without adhering to semantic versioning until a stable 1.x release.
- gotcha The completeness and accuracy of country/state data are dependent on the package's maintenance and upstream ISO 3166-2 updates. Some subdivisions might be missing or outdated.
Install
-
npm install country-states -
yarn add country-states -
pnpm add country-states
Imports
- compile
const { compile } = require('country-states');import { compile } from 'country-states'; - states
const { states } = require('country-states');import { states } from 'country-states'; - default import
import countryStates from 'country-states';
import * as countryStates from 'country-states';
Quickstart
import { compile, states } from 'country-states';
// It's crucial to call `compile` with the country codes you intend to use
// before attempting to retrieve states. This initializes the data for those countries.
// ISO 3166-1 alpha-2 codes are used here.
compile(['US', 'CA', 'DE', 'AU']);
console.log('States for United States (US):');
const usStates = states('US');
if (usStates && usStates.length > 0) {
console.log(` Total US states/territories: ${usStates.length}`);
console.log(' First 3 US states:', usStates.slice(0, 3).map(s => s.name).join(', '));
} else {
console.log(' No data found for US.');
}
console.log('\nStates for Germany (DE):');
const deStates = states('DE');
if (deStates && deStates.length > 0) {
console.log(` Total German states/territories: ${deStates.length}`);
console.log(' First 3 German states:', deStates.slice(0, 3).map(s => s.name).join(', '));
} else {
console.log(' No data found for DE. Did you include it in compile()?');
}
// Attempting to access a country not compiled will return undefined or an empty array.
console.log('\nStates for France (FR) without prior compilation:');
const frStates = states('FR');
if (frStates && frStates.length > 0) {
console.log(` Total FR states/territories: ${frStates.length}`);
} else {
console.log(' No data found for FR, as it was not included in compile().');
}
// You can inspect the structure of a state object
if (usStates && usStates.length > 0) {
console.log('\nExample state object (from US):', usStates[0]);
}