# is-localhost-ip

> Checks whether given DNS name or IPv4/IPv6 address belongs to a local machine

Latest version **3.0.1** (published 2026-01-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install is-localhost-ip
pnpm add is-localhost-ip
yarn add is-localhost-ip
bun add is-localhost-ip
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2026-01-18 |
| First published | 2019-08-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 8.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 27 |
| Author | Konstantin Vyatkin |
| Maintainers | tinovyatkin |
| Keywords | check-localhost, dns, ip, is-local-ip, is-localhost, is-loopback, localhost |

## Links

- npm: https://www.npmjs.com/package/is-localhost-ip
- Repository: https://github.com/tinovyatkin/is-localhost-ip
- Homepage: https://github.com/tinovyatkin/is-localhost-ip#readme
- Issues: https://github.com/tinovyatkin/is-localhost-ip/issues
- npm.io page: https://npm.io/package/is-localhost-ip

## Alternatives

- [lodash.startswith](https://npm.io/package/lodash.startswith.md) — 769.7K weekly downloads
- [@tarojs/service](https://npm.io/package/@tarojs/service.md) — 33.9K weekly downloads
- [io.extendreality.tilia.indicators.spatialtargets.unity](https://npm.io/package/io.extendreality.tilia.indicators.spatialtargets.unity.md) — 131 weekly downloads
- [@rtarojs/taro](https://npm.io/package/@rtarojs/taro.md) — 90 weekly downloads
- [node-branch-io](https://npm.io/package/node-branch-io.md) — 50 weekly downloads

## Recent versions

- 3.0.1 (latest) — 2026-01-18
- 3.0.0 — 2026-01-18
- 2.0.0 — 2022-08-03
- 1.4.0 — 2020-04-07
- 1.3.0 — 2019-08-26
- 1.2.0 — 2019-08-25
- 1.1.0 — 2019-08-23
- 1.0.0 — 2019-08-17

## README

[![codecov](https://codecov.io/gh/tinovyatkin/is-localhost-ip/branch/master/graph/badge.svg)](https://codecov.io/gh/tinovyatkin/is-localhost-ip)
![node](https://img.shields.io/node/v/is-localhost-ip)

# is-localhost-ip

Zero-dependency Node.js utility that checks whether a hostname or IPv4/IPv6 address refers to the local machine.

This package aims to be strict and comprehensive:

- Validates input as an IP address or a syntactically valid hostname (including bracketed IPv6).
- Treats private/loopback/link-local ranges as local.
- Optionally verifies the address exists on the current machine by attempting to bind to it.
- Falls back to DNS resolution, so it works with hostnames mapped in `/etc/hosts` or a local resolver.

## Installation

```sh
npm i is-localhost-ip
# or
yarn add is-localhost-ip
# or
pnpm add is-localhost-ip
```

Requires Node.js `>=18`.

## Usage

```js
const isLocalhost = require("is-localhost-ip");

(async () => {
  await isLocalhost("127.0.0.1"); // true
  await isLocalhost("::ffff:127.0.0.1"); // true
  await isLocalhost("192.168.0.12"); // true
  await isLocalhost("192.168.0.12", true); // true only if an interface has this address
  await isLocalhost("184.55.123.2"); // false

  await isLocalhost("tino.local"); // true if it resolves to a local address
  await isLocalhost("localhost"); // true
  await isLocalhost("microsoft.com"); // false
})();
```

## API

### `isLocalhost(ipOrHostname, canBind?)`

Returns a `Promise<boolean>`.

- `ipOrHostname` (`string`): IP address (v4/v6) or a hostname.
- `canBind` (`boolean`, default `false`): when `true`, additionally checks that the local machine can bind to the
  address (i.e., it is configured on a local interface).

The function throws for invalid inputs (non-string values or syntactically invalid hostnames).

## Caveats

Internationalized domain names (IDNs) are not supported. If you need IDNs, use
[Punycode.js](https://github.com/bestiejs/punycode.js) (or another punycode implementation) to convert the input
to ASCII before calling this function:

```js
const isLocalhost = require("is-localhost-ip");
const punycode = require("punycode");

(async () => {
  await isLocalhost(punycode.toASCII("свобода.рф")); // false
  await isLocalhost(punycode.toASCII("私の.家")); // true
})();
```

## License

`is-localhost-ip` is available under the [MIT](https://mths.be/mit) license.

---
_Source: https://npm.io/package/is-localhost-ip · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
