Git Product home page Git Product logo

internal-ip's Introduction

internal-ip

Get your internal IP address

Install

npm install internal-ip

Usage

import {internalIpV6, internalIpV4} from 'internal-ip';

console.log(await internalIpV6());
//=> 'fe80::1'

console.log(await internalIpV4());
//=> '10.0.0.79'

API

The package returns the address of the internet-facing interface, as determined from the default gateway. When the address cannot be determined for any reason, undefined will be returned.

The package relies on operating systems tools. On Linux and Android, the ip command must be available, which depending on distribution might not be installed by default. It is usually provided by the iproute2 package. internalIpV6Sync() and internalIpV4Sync() are not supported in browsers and just return undefined.

internalIpV6()

Returns the internal IPv6 address asynchronously.

internalIpV4()

Returns the internal IPv4 address asynchronously.

internalIpV6Sync()

Returns the internal IPv6 address synchronously.

internalIpV4Sync()

Returns the internal IPv4 address synchronously.

Related

internal-ip's People

Contributors

arthurvr avatar bendingbender avatar chriscindy avatar marsup avatar richienb avatar rodrigo54 avatar samverschueren avatar shellscape avatar silverwind avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

internal-ip's Issues

Must use import to load ES Module: /usr/src/app/node_modules/internal-ip/index.js

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /usr/src/app/node_modules/internal-ip/index.js
backend | require() of ES modules is not supported.
backend | require() of /usr/src/app/node_modules/internal-ip/index.js from /usr/src/app/dist/presentation/interceptors/LoggingInterceptor.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
backend | Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /usr/src/app/node_modules/internal-ip/package.json.

Node.js 4 support

I understand that nobody likes to support old versions. But:

  1. webpack-dev-server uses internal-ip (because this library is great)
  2. All my open source libraries can be tested on Travis CI in Node.js 4. yarn install fail with engines check error.
  3. Node.js 4 is still supported.

I check the sources and didn’t find critical parts for Node.js 6. Why we have engines limit in package.json?

@silverwind Maybe we can add Node.js 4 support with 1-2 lines? If we need big work, of course, I will try to find some other solution.

sync methods still relevant

This goes back a ways, but the changes to the API moving to an async, promise-based pattern in #9 are causing some headache with regard to updating. specifically, webpack-dev-server utilizes this module, and while a refactor to be more promise-friendly is in the works, it'll be some time before the project can use 2.x of internal-ip without a synchronous method. I'd like to suggest bringing back a synchronous method, even if it's not as accurate and contained a disclaimer, to allow use of the newer versions in apps/modules/projects that may not be able to leverage the updated async API as yet.

It would seem that @silverwind 's default-gateway module, and it's use of execa's async patterns is the sole reason that the API is 100% async. It would be reasonable to provide sync methods via default-gateway if that module would also leverage execa.sync

Of course, setting the dependency to the hard 1.2.0 version is always a solution. But providing synchronous methods here as well would be a big win.

Support the case of default gateway outside any subnet

The module currently assumes that the default gateway lies within one of the configured interface subnets but there are cases where this assumption does not hold true.

One example is a Travis VM having 10.20.0.153/32 with gateway 10.20.0.1. Strictly speaking, this is a invalid network configuration but it somehow still works so we need to account for it.

One approach could be to find the address nearest to the gateway. It's a guess but chances are it might be correct. To get the nearest IPv6 adress conveniently, we need to convert both the address CIDR and gateway address to a BigInt and then compare them, so I would like to require native BigInt (node 10.4.0+).

Need support commonjs!

Using in nestjs project, only support commonjs.

nestjs project based on typescript, after build the module import by require.

Remove fallbacks to ::1 or 127.0.0.1

I find the fallbacks to the localhost IPs don't really add any value and only complicate a user's code if they need a "real" IP. Should we remove them? It'd be a breaking change, but it's one I'm glad to take.

unicode vulnerability?

Hopefully this is the right 'internal-ip' package. Caught reference to a "unicode vulnerability" in this module via:
appcelerator/appc-pubsub#9
appcelerator/appc-pubsub@0d28f05

That project stopped using internal-ip due to the issue. Checking this repo, I don't see any indication of that and am wondering if this might be new to you, and/or if you were aware and there was more information available.

Not working on angular 8.3.20 on windows 10

Hi there!

I'm not getting this package to work on version 8.3.20 of angular, and windows 10.
Let me know if you need any more information.
I leave here the error, and my package.json, where you can see it does not compile.
image
package.json.zip

At last, how i am using it:

image
image

I tried import internalIp from 'internal-ip' as one issue suggested, but it didn't work so I had to do import * as internalIp from 'internal-ip'

Thanks in advance!

Report an error in my Electron-Vue

`
const internalIp = require('internal-ip')

void (async () => {
console.log(await internalIp.v6())
// => 'fe80::1'

console.log(await internalIp.v4())
// => '10.0.0.79'
})()

console.log(internalIp.v6.sync())
// => 'fe80::1'

console.log(internalIp.v4.sync())
// => '10.0.0.79'

`

SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:606:28)
at Object.Module._extensions..js (module.js:653:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:504:12)
at Function.Module._load (module.js:496:3)
at Module.require (module.js:586:17)
at require (internal/module.js:11:18)
at $refs.(anonymous function).validate.valid (file:///D:/02_work/instant-messaging-system/renderer/windows/Login/login-win-index.js:131:30)
He @ vue.min.js:6
lADPBbCc1f8bs2jNAkHNAkA_576_577.jpg Failed to load resource: net::ERR_CONNECTION_RESET

AIX as OS support

We are unable to install it on iSeries:

npm ERR! code EBADPLATFORM
npm ERR! notsup Unsupported platform for [email protected]: wanted {"os":"android,darwin,freebsd,linux,openbsd,sunos,win32","arch":"any"} (current: {"os":"aix","arch":"ppc64"})
npm ERR! notsup Valid OS: android,darwin,freebsd,linux,openbsd,sunos,win32
npm ERR! notsup Valid Arch: any
npm ERR! notsup Actual OS: aix
npm ERR! notsup Actual Arch: ppc64

Its a dependency for react.
Is there a way to support this os

Can't get IP Address on Windows 11

WMIC (the way default-gateway package gets the default gateway on Windows) was removed from Windows 10/11.

image

I'm trying to help people from default-gateway package to fix it as soon as possible.
Then, dependencies here may need to be updated.

use sync() with import ?

Hi there 😄

I tryed these :

import * as internalIp from 'internal-ip';

internalIp.v4().then(ip => console.log(ip)) // working

// console.log(internalIp.v4().sync()) // not working : Property 'sync' does not exist on type 'Promise<string>'

// console.log(internalIp.v4.sync()) // not working : Property 'sync' does not exist on type '() => Promise<string>'.

Any idea ?

Thanks

Issue with the node:os import.

The following import statement does not actually seem to work.

import {networkInterfaces} from 'node:os';

Switching to

import {networkInterfaces} from 'os';

does. Otherwise I get an ESM error.

Using node version v16.3.0 and npm version 7.15.1

require('internal-ip').v4.sync() returns null

Hi,

First of all, thanks for this awesome package. It has been working many months flawlessly, even on my machine, where many other packages seem to produce consistently or randomly wrong ip-addresses.

Today I upgraded to v.3.0.1.

On my PC (Windows 10), require('internal-ip').v4.sync() returns always null. I am not an expert on JS, but what I can see from putting a console.log into the internal-ip\index.js:

function sync(family) {
	try {
		const result = defaultGateway[family].sync();
		return findIp(result.gateway) || null;
	} catch (err) {
        console.log(err) <----- logging
		return null;
	}
}

is this:

TypeError: (gwTable || "").trim is not a function
    at parse (C:\MyProject\src\client\node_modules\default-gateway\win32.js:12:19)
    at sync (C:\MyProject\src\client\node_modules\default-gateway\win32.js:56:10)
    at Function.module.exports.v4.sync (C:\MyProject\src\client\node_modules\default-gateway\win32.js:62:32)
    at Function.module.exports.v4.sync (C:\MyProject\src\client\node_modules\default-gateway\index.js:17:46)
    at sync (C:\MyProject\src\client\node_modules\internal-ip\index.js:36:41)
    at Function.module.exports.v4.sync (C:\MyProject\src\client\node_modules\internal-ip\index.js:48:32)
    at Object.<anonymous> (C:\MyProject\src\client\server.js:8:27)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

the version of default-gateway is 2.6.1. Is this a problem of default-gateway or interal-ip? I should also mention, that the "Promise version" (require('internal-ip').v4()) does work correctly.

Update:
Downgraded to internal-ip v.1.2.0 and it works fine; just using it as var ip = require('internal-ip')().

internalIp.v4 is not a function

TypeError: internalIp.v4 is not a function
    at Server.<anonymous> (C:\ftpsrv\primitive-cloud-server\src\server.js:41:40)
    at Object.onceWrapper (node:events:509:28)
    at Server.emit (node:events:402:35)
    at emitListeningNT (node:net:1368:10)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)

networkInterfaces key `utun0` is not a ip

> os.networkInterfaces()
{ lo0:
   [ { address: '::1',
       netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
       family: 'IPv6',
       mac: '00:00:00:00:00:00',
       scopeid: 0,
       internal: true },
     { address: '127.0.0.1',
       netmask: '255.0.0.0',
       family: 'IPv4',
       mac: '00:00:00:00:00:00',
       internal: true },
     { address: 'fe80::1',
       netmask: 'ffff:ffff:ffff:ffff::',
       family: 'IPv6',
       mac: '00:00:00:00:00:00',
       scopeid: 1,
       internal: true } ],
  en0:
   [ { address: 'fe80::3e15:c2ff:fedf:58ea',
       netmask: 'ffff:ffff:ffff:ffff::',
       family: 'IPv6',
       mac: '3c:15:c2:df:58:ea',
       scopeid: 4,
       internal: false },
     { address: '192.168.199.134',
       netmask: '255.255.255.0',
       family: 'IPv4',
       mac: '3c:15:c2:df:58:ea',
       internal: false } ],
  awdl0:
   [ { address: 'fe80::b85a:4dff:fe78:452',
       netmask: 'ffff:ffff:ffff:ffff::',
       family: 'IPv6',
       mac: 'ba:5a:4d:78:04:52',
       scopeid: 8,
       internal: false } ],
  utun0:
   [ { address: '30.39.56.182',
       netmask: '255.255.255.255',
       family: 'IPv4',
       mac: '00:00:00:00:00:00',
       internal: false } ] }
internalIp.v4() => 30.39.56.182

Find a better method

Given

  en0: [ { address: 'x.x.x.x', netmask: '255.255.255.0', family: 'IPv4', internal: false } ],
  vboxnet0: [ { address: 'y.y.y.y', netmask: '255.255.255.0', family: 'IPv4', internal: false } ]

the module will wrongly return the VM interface's address. I think we can do better, maybe by utilizing the machine's routing table to find the default gateway.

Angular 10 CommonJS Warning and Module Not Found Errors

So I have setup a new test project to see how this works. Now the clean build well worked as expected, but when adding this module and add the code in I received following messages:

WARNING in D:\IKANDA\test\ipaddresstest\src\app\app.component.ts depends on 'internal-ip'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

ERROR in ./node_modules/cross-spawn/index.js
Module not found: Error: Can't resolve 'child_process' in 'D:\IKANDA\test\ipaddresstest\node_modules\cross-spawn'

ERROR in ./node_modules/execa/index.js
Module not found: Error: Can't resolve 'child_process' in 'D:\IKANDA\test\ipaddresstest\node_modules\execa'

ERROR in ./node_modules/cross-spawn/lib/util/readShebang.js
Module not found: Error: Can't resolve 'fs' in 'D:\IKANDA\test\ipaddresstest\node_modules\cross-spawn\lib\util'

ERROR in ./node_modules/isexe/index.js
Module not found: Error: Can't resolve 'fs' in 'D:\IKANDA\test\ipaddresstest\node_modules\isexe'

ERROR in ./node_modules/isexe/windows.js
Module not found: Error: Can't resolve 'fs' in 'D:\IKANDA\test\ipaddresstest\node_modules\isexe'

ERROR in ./node_modules/isexe/mode.js
Module not found: Error: Can't resolve 'fs' in 'D:\IKANDA\test\ipaddresstest\node_modules\isexe'

ERROR in ./node_modules/default-gateway/android.js
Module not found: Error: Can't resolve 'net' in 'D:\IKANDA\test\ipaddresstest\node_modules\default-gateway'

ERROR in ./node_modules/default-gateway/win32.js
Module not found: Error: Can't resolve 'net' in 'D:\IKANDA\test\ipaddresstest\node_modules\default-gateway'

ERROR in ./node_modules/default-gateway/sunos.js
Module not found: Error: Can't resolve 'net' in 'D:\IKANDA\test\ipaddresstest\node_modules\default-gateway'

ERROR in ./node_modules/default-gateway/linux.js
Module not found: Error: Can't resolve 'net' in 'D:\IKANDA\test\ipaddresstest\node_modules\default-gateway'

ERROR in ./node_modules/default-gateway/openbsd.js
Module not found: Error: Can't resolve 'net' in 'D:\IKANDA\test\ipaddresstest\node_modules\default-gateway'

ERROR in ./node_modules/default-gateway/freebsd.js
Module not found: Error: Can't resolve 'net' in 'D:\IKANDA\test\ipaddresstest\node_modules\default-gateway'

ERROR in ./node_modules/default-gateway/darwin.js
Module not found: Error: Can't resolve 'net' in 'D:\IKANDA\test\ipaddresstest\node_modules\default-gateway'

ERROR in ./node_modules/default-gateway/index.js
Module not found: Error: Can't resolve 'os' in 'D:\IKANDA\test\ipaddresstest\node_modules\default-gateway'

ERROR in ./node_modules/default-gateway/win32.js
Module not found: Error: Can't resolve 'os' in 'D:\IKANDA\test\ipaddresstest\node_modules\default-gateway'

ERROR in ./node_modules/default-gateway/linux.js
Module not found: Error: Can't resolve 'os' in 'D:\IKANDA\test\ipaddresstest\node_modules\default-gateway'

ERROR in ./node_modules/default-gateway/darwin.js
Module not found: Error: Can't resolve 'os' in 'D:\IKANDA\test\ipaddresstest\node_modules\default-gateway'

ERROR in ./node_modules/execa/lib/kill.js
Module not found: Error: Can't resolve 'os' in 'D:\IKANDA\test\ipaddresstest\node_modules\execa\lib'

ERROR in ./node_modules/human-signals/build/src/main.js
Module not found: Error: Can't resolve 'os' in 'D:\IKANDA\test\ipaddresstest\node_modules\human-signals\build\src'

ERROR in ./node_modules/human-signals/build/src/signals.js
Module not found: Error: Can't resolve 'os' in 'D:\IKANDA\test\ipaddresstest\node_modules\human-signals\build\src'

ERROR in ./node_modules/internal-ip/index.js
Module not found: Error: Can't resolve 'os' in 'D:\IKANDA\test\ipaddresstest\node_modules\internal-ip'

ERROR in ./node_modules/cross-spawn/lib/parse.js
Module not found: Error: Can't resolve 'path' in 'D:\IKANDA\test\ipaddresstest\node_modules\cross-spawn\lib'

ERROR in ./node_modules/cross-spawn/lib/util/resolveCommand.js
Module not found: Error: Can't resolve 'path' in 'D:\IKANDA\test\ipaddresstest\node_modules\cross-spawn\lib\util'

ERROR in ./node_modules/cross-spawn/node_modules/which/which.js
Module not found: Error: Can't resolve 'path' in 'D:\IKANDA\test\ipaddresstest\node_modules\cross-spawn\node_modules\which'

ERROR in ./node_modules/execa/index.js
Module not found: Error: Can't resolve 'path' in 'D:\IKANDA\test\ipaddresstest\node_modules\execa'

ERROR in ./node_modules/npm-run-path/index.js
Module not found: Error: Can't resolve 'path' in 'D:\IKANDA\test\ipaddresstest\node_modules\npm-run-path'

ERROR in ./node_modules/execa/node_modules/get-stream/buffer-stream.js
Module not found: Error: Can't resolve 'stream' in 'D:\IKANDA\test\ipaddresstest\node_modules\execa\node_modules\get-stream'

ERROR in ./node_modules/merge-stream/index.js
Module not found: Error: Can't resolve 'stream' in 'D:\IKANDA\test\ipaddresstest\node_modules\merge-stream'

This is how my component looks like:

import { Component } from '@angular/core';
import * as internalIp from "internal-ip";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'ipaddresstest';

  ngOnInit(){
    (async () => {
      console.log(await internalIp.v6());
      //=> 'fe80::1'
    
      console.log(await internalIp.v4());
      //=> '10.0.0.79'
    })();
    
    console.log(internalIp.v6.sync())
    //=> 'fe80::1'
    
    console.log(internalIp.v4.sync())
    //=> '10.0.0.79'
  }
}

Even if I were to move this it still gives those errors, any ideas on how to solve them??

Thanks in Advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.