DEV Community

loading...

How To Get Client IP with Javascript / React

calvinpak profile image CP Updated on ・1 min read

With VPN and Tor, IP may not be a reliable data anymore. Still there are times when we want to log that info. To my surprise, there is no built-in ReactJS (please correct me if I'm wrong about this) method that displays the client IP address. Definitely, you can retrieve that info from the request object "manually".

After trying a few popular packages, the simplest one that does the job is public-ip, super simple, and it provides support for fallback URLs when the IP is not found in the request.

import publicIp from "public-ip";

export const getClientIp = async () => await publicIp.v4({
  fallbackUrls: [ "https://ifconfig.co/ip" ]
});
Enter fullscreen mode Exit fullscreen mode

Discussion

pic
Editor guide