Is there a way to dynamically set the IP address in my npm script so I dont have to?

I have a start script:

"start": "meteor run --port 111.111.1.111:3000 --settings settings-local.json",

I like using the IP instead of localhost, then I can connect my iphone and debug webapps by mobile, or I can have my expo react-native app hit the local version of the server code.

But every time I switch locations (home/office) I have to remember to go in my script and change out the hard coded address (it changes depending on whos wifi you’re on). Is there a variable or something that I can plug into my start script so it just grabs whatever the IP is?

It’s not the end of the world but it’s one of those things where you’re like “there has to be a better way”

I know you can get the IP in the code, but I need it in the start script.

You can try this:

"scripts": {
    "start": "meteor run --port `ip route get 8.8.8.8 | awk '{print $NF; exit}'`:3000 --settings settings-local.json"
 }

To test, run this from your command line to ensure you get the correct ip address:

ip route get 8.8.8.8 | awk '{print $NF; exit}'

on a mac right now …

I know this is a pretty old thread, but I have the exact same problem (on a Mac). Does anyone know a solution for this?

I am developing at multiple locations, but need to provide Meteor with a real IP (localhost won’t work). Every time I switch my environment, I have to patch package.json again to set the correct IP.

You could use a dynamic DNS service. Since I have my own domain names with namecheap, I use their service, for which they don’t charge anything. Also with them, you can use your browser to set your domain (or just a host thereof) to your currently assigned IP address dynamically.

1 Like

Our DevOps created a script that is executed together when starting meteor, e.g. meteor npm run web

I check the script, and it uses the command /sbin/ifconfig to extract the IP of your machine in your LAN (we use both Ubuntu and Mac dev machines)

Thanks, I know. But I would prefer a solution that works across the whole dev team, without additional configuration, if this is possible.

Thanks. Yes, this can be done via shell scripting, but I would prefer a solution that works across operating systems, in our case Windows and Mac. Couldn’t find a “npm only” solution so far, though.

In our case, the dev instance running on my mac is reachable from the outside as peter.mydomain.com, my colleague’s as tom.mydomain.com and so on; so it’s a team solution (almost) without additional configuration. The only thing each of us had to set once is an env var with that name in .bash_profile.

ifconfig in unix, ipconfig in windows.

$OSType can detect the OS in most shell environment

I wrote a utility called askmike some time ago, which may help you here. You can run it easily with

npx askmike ip

and it will print your IP address.

The code behind is on GitHub - mikkelking/askmike: Command line utilities to get useful information (starting with ip address), or the code snippet is here:

const os = require('os')

const getip = () => {
  const ifaces = os.networkInterfaces()
  const results = []

  Object.keys(ifaces).forEach(function(ifname) {
    let alias = 0

    ifaces[ifname].forEach(function(iface) {
      if ('IPv4' !== iface.family || iface.internal !== false) {
        // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
        return
      }

      if (alias >= 1) {
        // this single interface has multiple ipv4 addresses
        results.push(`${ifname}(${alias}): ${iface.address}`)
      } else {
        // this interface has only one ipv4 adress
        results.push(`${ifname}: ${iface.address}`)
      }
      ++alias
    })
  })
  return results
}

module.exports = getip

I believe it is os independent

1 Like

There is a service named ngrok: https://ngrok.com/