Or if not tied to just using built in methods why not use this resource https://ipinfo.io/json and when you hit that address using your code you will get back a json object like below and can use that data in your code. Just use fetch to call the api and I hope it does work for client request and not your servers ip.
Keep in mind that many (most?) users don’t have static IPs. That means that their IP could change each time they visit the site. Not sure how this will impact your site, but you should at be aware of this.