Find User Geolocation from IP Address in Node.js

It is possible to identify where your users are logging in from with great insights and better user experience. Node.js makes it easy to find geolocation from IP address and apply it for analytics or personalization. You could, for instance, show currency depending on the region of the user, auto-complete location fields, or limit access to specific regions.

By knowing how to obtain location from IP address in Node.js, you can create smarter, more responsive applications that automatically adjust to users. Let’s see how to accomplish this through APIs and libraries in various ways.

Why Geolocation Is Used

Geolocation helps developers in providing personalized and safe experiences. Some typical use cases are:

  • Displaying region-specific content or prices.
  • Restricting geographic location-based access.
  • Measuring website or app usage analytics.
  • Boosting marketing with localized targeting.
  • Enabling precise delivery, weather, or timezone information.

Get Geolocation by IP Address Using IP API

The simplest and safest way to find geolocation by IP is by using an external API like ip-api.com
. These API providers JSON responses including country, city, latitude, and longitude with higher accuracy and latest data.

You can implement this method with just like any other API service use case. Here, we have to pass user’s IP address and it will return Geolocation data as a response. Let’s take example to see that.

import express from "express";
import fetch from "node-fetch";

const app = express();

app.get("/", async (req, res) => {
  const ip = req.headers["x-forwarded-for"] || req.socket.remoteAddress;
  const response = await fetch(`http://ip-api.com/json/${ip}`);
  const data = await response.json();
  res.json(data);
});

app.listen(3000, () => console.log("Server running on port 3000"));

This example will retrieves the client’s live IP and returns details such as city, region, and country directly from IP API and return it to browser.

Get User Location by IP Address Using Geoip-lite

For more simple approach, you can move forward with the geoip-lite library. The package look up IP locations locally without external API calls. It’s a fast and reliable option when you want to avoid network dependency.

To install this package, you need to run below npm command:

npm install geoip-lite

Once installed, you can start implementing it into your node js application like below example.

import express from "express";
import geoip from "geoip-lite";

const app = express();

app.get("/", (req, res) => {
  const ip = req.headers["x-forwarded-for"] || req.socket.remoteAddress;
  const geo = geoip.lookup(ip);
  res.json(geo);
});

app.listen(3000, () => console.log("Server running on port 3000"));

The package provides lookup method which return instant results by resolving the location using local data files instead of API requests. This approach is faster compared to API but API has more accuracy.

Get Geolocation Using Ipinfo.io API in Node.js

There are many APIs which helps you to find user geolocation information using node.js. Most of them are paid even for personal use cases. However, the Ipinfo.io offers free API calls for non-commersial uses. You just have to create an API key on their platform and pass this API key with every requests. The IPinfo is highly recommended service provider out there.

import express from "express";
import fetch from "node-fetch";

const app = express();

app.get("/", async (req, res) => {
  const ip = req.headers["x-forwarded-for"] || req.socket.remoteAddress;
  const response = await fetch(`https://ipinfo.io/${ip}/json?token=YOUR_TOKEN`);
  const data = await response.json();
  res.json(data);
});

app.listen(3000, () => console.log("Server running on port 3000"));

This approach is ideal for apps that require detailed and regularly updated geolocation data.

Conclusion

Understanding how to retrieve location from IP address in Node.js provides you with a number of opportunities to improve your application. You can use either a free API, geoip-lite library, or services such as Ipinfo.io, and all of them assist you in determining user regions effectively. With geolocation, you can retrieve address by IP, enhance security, provide localized content, and achieve a more customized user experience.

If you’re working with data-driven applications, you’ll also find our guide on Read and Write Excel Files in Node.js useful. It explains how to manage Excel sheets dynamically to store, process, or export data efficiently in your Node.js projects.