Registry / data / country-states

country-states

JSON →
library0.0.10jsnpmunverified

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.

npm install country-states
INSTALL
IMPORT
SIG · COUNTRY-STATES
C
country-states
datajavascriptv0.0.10
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

compile
import { compile } from 'country-states';
const { compile } = require('country-states');
While CommonJS `require` can be used in older Node.js environments, ESM `import` is the preferred and more idiomatic way to load this module in modern JavaScript and TypeScript projects. The `compile` function must be called once with desired country codes before state data can be accessed.
states
import { states } from 'country-states';
const { states } = require('country-states');
The `states` function retrieves the list of subdivisions for a specific country. It is essential that the target country's data has been initialized via a preceding call to `compile`.
default import
import * as countryStates from 'country-states';
import countryStates from 'country-states';
This package exports named symbols (`compile`, `states`) and does not provide a default export. Attempting a default import will result in `undefined` or an error, as there's no top-level default export.

This quickstart demonstrates how to initialize country data using `compile` and then retrieve specific state information using `states`, highlighting the importance of pre-compilation for desired country codes.

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]); }
Debug
Known issues
gotchaThe `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.
fix
Ensure `compile(['US', 'DE', ...])` is executed once at application startup or before any `states()` calls for relevant countries.
affects: >=0.0.1
breakingAs 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.
fix
Pin exact versions (e.g., `"country-states": "0.0.10"`) in `package.json` and manually test upgrades before deployment.
affects: <1.0.0
gotchaThe 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.
fix
For critical applications, cross-reference data with official ISO sources or governmental APIs to ensure accuracy and up-to-dateness.
affects: >=0.0.1
Errors
Common errors & fixes
TypeError: states is not a function
Incorrect import or require statement, or trying to access `states` before the module is properly loaded.
fix
Use `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 array
The country code 'XX' was not included in the array passed to the `compile` function, or the `compile` function was not called at all.
fix
Before calling `states('XX')`, ensure you have executed `compile(['XX', ...])` with 'XX' explicitly listed in the array of country codes.
Cannot find module 'country-states'
The package has not been installed or is not correctly resolved in your project's `node_modules`.
fix
Run `npm install country-states` or `yarn add country-states` in your project directory.
Upgrade
Version history
0.0.10latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources
country-states — npm install country-states · libregistry