Basic library for Country, State and City
Installations
npm install country-state-city
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
14.19.0
NPM Version
8.5.3
Releases
Reduced Bundle Size
Published on 20 Aug 2023
Minified JSON files - Country, State, City
Published on 19 Aug 2023
Data split to improve data fixing and better debugging
Published on 21 Aug 2022
Tree Shaking support - Release : v3.0.0(Not backward compatible)
Published on 24 Jun 2021
Release v2.0.0
Published on 05 Jan 2021
First Major Version Release
Published on 28 May 2019
Contributors
Languages
TypeScript (99.3%)
JavaScript (0.7%)
Developer
Download Statistics
Total Downloads
10,029,090
Last Day
20,220
Last Week
82,335
Last Month
369,011
Last Year
4,127,925
GitHub Statistics
287 Stars
207 Commits
184 Forks
7 Watching
32 Branches
13 Contributors
Bundle Size
8.31 MB
Minified
2.23 MB
Minified + Gzipped
Package Meta Information
Latest Version
3.2.1
Package Id
country-state-city@3.2.1
Unpacked Size
16.69 MB
Size
4.39 MB
File Count
33
NPM Version
8.5.3
Node Version
14.19.0
Publised On
18 Sept 2023
Total Downloads
Cumulative downloads
Total Downloads
10,029,090
Last day
-4.2%
20,220
Compared to previous day
Last week
-17.1%
82,335
Compared to previous week
Last month
3.6%
369,011
Compared to previous month
Last year
39%
4,127,925
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
country-state-city
Basic library for Country, State and City
Data Source:
https://github.com/dr5hn/countries-states-cities-database
For any data related issue, you can also raise a Issue here.
Install
npm i country-state-city
Demos - Tree Shaking
Release Info & Documentation
- Release v3.2.0
- To optimize bundle size, the data JSON files have undergone minification. For instance, the
city.json
file, which was originally around 25MB in an unminified state, has now been reduced to approximately 8MB through minification. - The structure of the
city.json
file has been transformed from an Array of Objects to an Array of Arrays of Strings. Previously, the file contained duplicate property names for each object, resulting in a minified file size of 16MB due to JSON formatting requirements. To further minimize this, the file structure has been converted to an Array of Arrays, astonishingly reducing the size to 8MB. - To accommodate this updated structure, modifications were made to the
city.ts
file. Importantly, these changes do not affect the functionality of existing code. All existing code will continue to work seamlessly without any breaking changes.
- To optimize bundle size, the data JSON files have undergone minification. For instance, the
- Release v3.1.0
- Usage
- Docs
Release : v3.1.0
Overview
- The data folder in the root contains all the data of Countries, their States and Cities
- The earlier large big JSONs are split into smaller JSONs and stored in separate files and folder
- The advantage of doing so is to make it easy to check, test and submit the change. It's easier to review a small file than a large file. It's easier to debug any missing or wrong values in a small file.
- This will help in future optimisation
- There is a separate folder for each country
- Each Country folder contains Country's information and its states as sub-child folders
- The Country's info is also split into - Country Lite, Plain, Geo, TimeZone's.
- Inside each Country - there are Sub-folder of State
- Each State folder contains State's information and its Cities as sub-child files
- State's info is also split into - State Lite, Geo
- Inside each State - there are JSON files of City
- Each City file contains information of all the Cities of that particular State
- Cities info is split into two files - City Lite, Geo
How to contribute to Country, State, City Database, request changes and report issue?
- Code change - make the necessary change, raise the PR.
- Data changes - Database is in
data
folder in the root. Please follow this guidelines. - Update: Add the necessary changes in the particular files with proper structure and format and then raise the PR.
- Add New - Country, State, City - follow the proper folder structure and raise the P.R or simply share the JSONs in an issue with proper and up to date reference.
- Deleting - Country, State, City - remove the necessary files/folder - update the necessary files and raise the PR or simply share the updated JSONs in an issue with a proper and up to date reference.
- Queries/Issues related to wrong data must be provided with some references.
- For any Geo-Political related queries, border conflicts - things will be updated when multiple proper references has been provided.
Data updation guide
Please follow this guidelines
How Data will be Released finally?
- After the changes gets merged, a script will be run to combine all the data in a separate but single file for each Country, State, City as we have earlier without affecting out library functions and only those three files are passed to the build code and then it gets released.
- In future we will be working on how to use these split files to optimise this library further.
Usage
Release : v3.0.0
(Major Version Release - Not backward compatible)
-
ES6 Module usage
1// Latest version - v3.0.0 with Tree Shaking to reduce bundle size 2import { Country, State, City } from 'country-state-city'; 3console.log(Country.getAllCountries()) 4console.log(State.getAllStates()) 5 6// Import Interfaces` 7import { ICountry, IState, ICity } from 'country-state-city' 8
-
AMD Module usage
1// Latest version - v3.0.0 2let Country = require('country-state-city').Country; 3let State = require('country-state-city').State; 4 5console.log(Country.getAllCountries()) 6console.log(State.getAllStates()) 7
For versions below v2.2.0 and above v0.1.8
-
ES6 Module usage
1import csc from 'country-state-city';
-
AMD Module usage
1let csc = require('country-state-city').default;
Docs
Country.getCountryByCode(countryCode)
It accepts a valid CountryCode
eg: 'AS'
and returns Country Details
type: json | ICountry
1{ 2 "isoCode": "IN", 3 "name": "India", 4 "phonecode": "91", 5 "flag": "🇮🇳", 6 "currency": "INR", 7 "latitude": "20.00000000", 8 "longitude": "77.00000000", 9 "timezones": [ 10 { 11 "zoneName": "Asia\/Kolkata", 12 "gmtOffset": 19800, 13 "gmtOffsetName": "UTC+05:30", 14 "abbreviation": "IST", 15 "tzName": "Indian Standard Time" 16 } 17 ] 18}
State.getStateByCodeAndCountry(stateCode, countryCode)
It accepts a valid StateCode
eg: 'TG'
CountryCode
eg: 'IN'
and returns State Details
type: json | ICountry
1{ 2 "name": "Telangana", 3 "isoCode": "TG", 4 "countryCode": "IN", 5 "latitude": "18.11243720", 6 "longitude": "79.01929970" 7}
State.getStatesOfCountry(countryCode)
It accepts a valid CountryCode
and returns all States as Array of JSON
type: array of json | IState
1[ 2 { 3 "name": "Delhi", 4 "isoCode": "DL", 5 "countryCode": "IN", 6 "latitude": "28.70405920", 7 "longitude": "77.10249020" 8 } 9] 10
City.getCitiesOfState(countryCode, stateCode)
It accepts a valid CountryCode
, StateCode
and returns all Cities as Array of JSON
type: array of json | ICity
1[ 2 { 3 "name": "New Delhi", 4 "countryCode": "IN", 5 "stateCode": "DL", 6 "latitude": "28.63576000", 7 "longitude": "77.22445000" 8 } 9] 10
City.getCitiesOfCountry(countryCode)
It accepts a valid CountryCode
and returns all Cities as Array of JSON
type: array of json | ICity
1[ 2 { 3 "name": "New Delhi", 4 "countryCode": "IN", 5 "stateCode": "DL", 6 "latitude": "28.63576000", 7 "longitude": "77.22445000" 8 } 9] 10
Country.getAllCountries
It returns all Countries
type: array of json | ICountry
1[ 2 { 3 "isoCode": "IN", 4 "name": "India", 5 "phonecode": "91", 6 "flag": "🇮🇳", 7 "currency": "INR", 8 "latitude": "20.00000000", 9 "longitude": "77.00000000", 10 "timezones": [ 11 { 12 "zoneName": "Asia\/Kolkata", 13 "gmtOffset": 19800, 14 "gmtOffsetName": "UTC+05:30", 15 "abbreviation": "IST", 16 "tzName": "Indian Standard Time" 17 } 18 ] 19 } 20]
State.getAllStates
It returns all States
type: array of json | IState
1[ 2 { 3 "name": "Delhi", 4 "isoCode": "DL", 5 "countryCode": "IN", 6 "latitude": "28.70405920", 7 "longitude": "77.10249020" 8 9 } 10]
City.getAllCities
It returns all Cities
type: array of json | ICity
1[ 2 { 3 "name": "Delhi", 4 "isoCode": "DL", 5 "countryCode": "IN", 6 "latitude": "28.70405920", 7 "longitude": "77.10249020" 8 } 9]
Special Thanks
@baywet - For mentoring Javascript to Typescript Conversion
@dr5hn - For updated World Data Dictionary
taylorthurlow - For pointing into right direction - Module Tree Shaking
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: GNU General Public License v3.0: LICENSE:0
Reason
no binaries found in the repo
Reason
Found 2/16 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'develop'
- Warn: branch protection not enabled for branch 'master'
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:9: update your workflow using https://app.stepsecurity.io/secureworkflow/harpreetkhalsagtbit/country-state-city/ci.yml/develop?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yml:13
- Info: 0 out of 1 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 29 are checked with a SAST tool
Reason
10 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-hc6q-2mpp-qw7j
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2.6
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to country-state-city
country-state-city-nextjs
Get all countrys, states and citys of world
thente-country-state-city
Forked country-state-city Library for fetching Country, its States and Cities
@ixulabs/country-state-city-library
Library for fetching Country, its States and Cities
react-country-state-city
A lightweight and easy-to-use React library that provides a comprehensive list of regions or continents, countries, states, cities languages, and country phone codes for creating dynamic and searchable dropdowns. Ideal for building forms and input fields