JoeTidee
JoeTidee

Reputation: 26054

package.json - how to get IP address of local machine?

I have a node.js application that uses webpack-dev-server, so in my package.json I have this to start it:

...
"scripts": {
    "start": "webpack-dev-server --host 192.168.0.13 --port 3001",
}
...

192.168.0.13 is my local IP address. As my network uses DHCP, is there a variable that I can use instead of my IP address in package.json or webpack.config.js file, so that it will always use my local IP?

Note: I have read that I can use 0.0.0.0, but this is apparently insecure, so is there something else I can use directly in my package.json or webpack.config.js file that will dynamically provide webpack-dev-server with my local IP address?

Upvotes: 4

Views: 4083

Answers (2)

iuliu.net
iuliu.net

Reputation: 7145

You can get your IP address inside of package.json with the ipconfig getifaddr en0 command like this:

"start": "webpack-dev-server --host \"$(ipconfig getifaddr en0)\" --port 3001"

Upvotes: 0

JoeTidee
JoeTidee

Reputation: 26054

I discovered that the host can be dynamically set within webpack.config.js using the host option:

...
devServer: {
    host: require('os').hostname().toLowerCase(),
    port: 3001,
    ...
},
...

Upvotes: 2

Related Questions