Error: Cannot find module 'ip'是由于没有改模块的依赖包,需要进行安装,以管理员的身份打开命令行,执行npm install ip
获取当前电脑的ip地址
方法一:
const ip = require("ip")/*** 1:获取当前电脑的ip地址*/
console.log(ip.address());
方法二:
const os = require('os');/*** 2:获取当前电脑的ip地址*/
function getIpAddress() {var ifaces=os.networkInterfaces()for (var dev in ifaces) {let iface = ifaces[dev]for (let i = 0; i < iface.length; i++) {let {family, address, internal} = iface[i]if (family === 'IPv4' && address !== '127.0.0.1' && !internal) {return address}}}
}let ipAddress = getIpAddress()
console.log(ipAddress)
执行代码node + js文件(例如:代码写在getIp.js文件中,进入js文件的上级目录,执行node getIp.js)
输出本机ip:10.10.10.128