https://github.com/k-yle/get-region
🌐 Tiny package (<400 bytes!) to get the user's country without calling any APIs, and without hardcoding any data.
https://github.com/k-yle/get-region
country intl npm region timezones
Last synced: about 1 month ago
JSON representation
🌐 Tiny package (<400 bytes!) to get the user's country without calling any APIs, and without hardcoding any data.
- Host: GitHub
- URL: https://github.com/k-yle/get-region
- Owner: k-yle
- License: mit
- Created: 2025-05-02T11:21:40.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2025-05-02T11:23:25.000Z (5 months ago)
- Last Synced: 2025-08-04T04:00:03.552Z (2 months ago)
- Topics: country, intl, npm, region, timezones
- Language: TypeScript
- Homepage: https://npm.im/get-region
- Size: 54.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# get-region
[](https://github.com/k-yle/get-region/actions)
[](https://npm.im/get-region)
[](https://npm.im/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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/resolvedOptions) to get the user's timezone, for example `Asia/Shanghai` or `Europe/Dublin`.
A [different `Intl` SDK](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getTimeZones) 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl).## Example
```js
import region from 'get-region';console.log(region.country); // for example: 'US' if you're in the United States
```## Install
```sh
npm install get-region
```## Questions
This library only gives you a country code.
- _What if I need the country name?_ — use the native [`Intl.DisplayNames` class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames)
- _What if I need the latitude/longitude?_ — use a library like [country-coder](https://github.com/rapideditor/country-coder).