Search code examples
javascriptnode.jsreactjsip

How to find out Client's private ip in React or Node js?


Problem

Currently I need to compare the user's private ip with the ip of the certificate.

Is there a way to find out the user's private ip in react or node?

I tried

I got a public ip but not a private ip

/* in React.js */
    const getIP = async () => {
        const res = await axios.get('https://geolocation-db.com/json/');
        console.log("getIP : ",res.data);
        setCurrentIP(res.data.IPv4);
    }

    useEffect(()=>{
        getIP();
    },[]);

Solution

  • Option 1 ->

    Any IP address of your machine you can find by using the os module - and that's native to Node.js:

    const os = require('os');
    
    const networkInterfaces = os.networkInterfaces();
    const ip = networkInterfaces['eth0'][0]['address']
    
    console.log(networkInterfaces);
    

    Option 2 ->

    You might like to use npm package npm install internal-ip

    import {internalIpV6, internalIpV4} from 'internal-ip';
    
    console.log(await internalIpV6());
    //=> 'fe80::1'
    
    console.log(await internalIpV4());
    //=> '10.0.0.79'
    

    AND FOR REFERENCE FINS THE LINK BELOW. HOPE IT HELPS

    Option 3 -> YouTube

    Web App to check IP

    GitHub Source Code

    Some additional read you might like ->

    For React.js -> Find Client's IP Address in React.js

    For Node.js -> Client's IP Address in Node.js