Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 726
JoeTidee Avatar asked Sep 18 '25 18:09

JoeTidee


1 Answers

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,
    ...
},
...
like image 51
JoeTidee Avatar answered Sep 21 '25 07:09

JoeTidee