get-device-ip 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +23 -0
  2. package/script.js +42 -0
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "get-device-ip",
3
+ "version": "1.0.0",
4
+ "description": "A lightweight utility to get local IPv4 and IPv6 addresses.",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": "./index.js"
9
+ },
10
+ "author": "Sakib Fakir",
11
+ "license": "MIT",
12
+ "engines": {
13
+ "node": ">=12.0.0"
14
+ },
15
+ "keywords": [
16
+ "ip",
17
+ "ipv4",
18
+ "ipv6",
19
+ "local-ip",
20
+ "device-ip",
21
+ "network"
22
+ ]
23
+ }
package/script.js ADDED
@@ -0,0 +1,42 @@
1
+ import os from 'os';
2
+ // Sakib Fakir
3
+ // published : 1/31/2026
4
+
5
+
6
+ const getDeviceIP = () => {
7
+ const IPv4 = [];
8
+ const IPv6 = [];
9
+
10
+ try {
11
+ const deviceInfo = os.networkInterfaces();
12
+
13
+ for (const interfaceName in deviceInfo) {
14
+ deviceInfo[interfaceName].forEach((addr) => {
15
+ // Skip internal (loopback) addresses
16
+ if (addr.internal) return;
17
+
18
+ // Check for IPv4 (supports string 'IPv4' or number 4)
19
+ if (addr.family === 'IPv4' || addr.family === 4) {
20
+ IPv4.push(addr.address);
21
+ }
22
+ // Check for IPv6 (supports string 'IPv6' or number 6)
23
+ else if (addr.family === 'IPv6' || addr.family === 6) {
24
+ IPv6.push(addr.address);
25
+ }
26
+ });
27
+ }
28
+
29
+ return {
30
+ IPv4,
31
+ IPv6,
32
+ primaryIpv4: IPv4[0] || null,
33
+ primaryIpv6: IPv6[0] || null
34
+ };
35
+
36
+ } catch (error) {
37
+
38
+ return { error: error.message, IPv4: [], IPv6: [] };
39
+ }
40
+ }
41
+
42
+ export default getDeviceIP;