get-region
v1.0.0
Published
🌐 Tiny package to get the user's country without calling any APIs (browser & nodejs)
Downloads
219
Readme
get-region
🌐 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.
It only works in modern browsers that support Intl.Locale#getTimeZones (or the legacy equivalent: Intl.Locale#timeZones).
Example
import region from 'get-region';
console.log(region.country); // for example: ['US'] if you're in the United States
// The API returns an array, but the array will only contain a single country code.
// This may change one day, depending on future geopolitical changes.Install
npm install get-regionQuestions
This library only gives you a country code.
- What if I need the country name? — use the native
Intl.DisplayNamesclass - What if I need the latitude/longitude? — use a library like country-coder.
