š Tiny package to get the user's country without calling any APIs (browser & nodejs)
npm install get-region


!npm bundle size
š Tiny package (<400 bytes!) to get the user's country without calling any APIs, and without hardcoding any data. Works in the browser and nodejs.
It uses the browser's native Intl SDK to get the user's timezone, for example Asia/Shanghai or Europe/Dublin.
A different Intl SDK is used to convert the timezone to a country code, such as CN or IE.
This is not always reliable; there are many cases where this approach fails.
However, it can provide a quick solution.
It also only works in modern browsers that support these Intl APIs.
``js
import region from 'get-region';
console.log(region.country); // for example: 'US' if you're in the United States
`
`sh`
npm install get-region
This library only gives you a country code.
- _What if I need the country name?_ ā use the native Intl.DisplayNames` class
- _What if I need the latitude/longitude?_ ā use a library like country-coder.