# extract-domain

> Extract domain name from URL

Latest version **5.0.2** (published 2024-04-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install extract-domain
pnpm add extract-domain
yarn add extract-domain
bun add extract-domain
```

## Health

**Score 28/100 (F)** — status: abandoned.

Positive: has types package; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.2 |
| Published | 2024-04-09 |
| First published | 2017-01-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/extract-domain) |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 230.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 30 |
| Author | Bjarne Oeverli |
| Maintainers | bjarneo |
| Keywords | extract, domain, name, parse, get, fetch, string, url, browser, node |

## Links

- npm: https://www.npmjs.com/package/extract-domain
- Repository: https://github.com/bjarneo/extract-domain
- Issues: https://github.com/bjarneo/extract-domain/issues
- npm.io page: https://npm.io/package/extract-domain

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 5.0.2 (latest) — 2024-04-09
- 5.0.1 — 2024-04-09
- 5.0.0 — 2024-04-09
- 4.1.5 — 2023-09-19
- 4.1.4 — 2023-09-02
- 4.1.3 — 2023-09-02
- 4.1.2 — 2023-09-02
- 4.1.0 — 2023-08-05
- 3.0.1 — 2023-07-28
- 3.0.0 — 2023-07-20
- 2.4.8 — 2022-07-16
- 2.4.6 — 2022-07-16
- 2.4.5 — 2022-07-16
- 2.4.4 — 2022-07-16
- 2.4.3 — 2022-07-16
- … 24 more at https://npm.io/package/extract-domain/versions

## README

# Extract domain name from URL

[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=4JDQMB6MRJXQE&source=url)

This package provides a performant way to extract domain names from URLs without using regular expressions or array manipulations.

Learn more about [What is a URL](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL)

## Supports

-   ESM
-   Node.js
-   Browser

## Usage

### Installation

```bash
$ npm i --save extract-domain
```

### Development

```
# Install bun https://bun.sh/
curl -fsSL https://bun.sh/install | bash


# tests
bun test:watch
```

### API

-   @param {Urls} urls ["https://www.google.com", "https://www.github.com"] or "https://www.google.com"
-   @param {GetDomainOptions} opts `{ tld: true }` permit to get Top Level Domain like `*.co.uk`
-   @returns {Urls | Promise<Urls>} Returns URL(s) or a promise of URL(s) if the PSL lib is being used

ESM import

```js
import extractDomain from 'extract-domain';
```

Examples

```js
const urls = [
    'https://www.npmjs.com/package/extract-domain',
    'http://www.example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument',
    'http://user:password@example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument',
    'https://npmjs.com/package/extract-domain',
    'ftp://example.org/resource.txt',
    'http://example.co.uk/',
    'this.is.my@email.com',
];

extractDomain(urls[0]); // npmjs.com

extractDomain(urls); // [ 'npmjs.com', 'example.com', 'example.com', 'npmjs.com', 'example.org', 'co.uk', 'email.com' ]
```

## TLD support

TLD support requires the optional dependency of the [`psl` library](https://www.npmjs.com/package/psl).

Examples

```bash
npm i --save-optional psl
```

```js
const url =
    'http://www.example.co.uk:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument';

async function extract(url) {
    console.log(await extractDomain(url, { tld: true }));
    // example.co.uk
}

// Or
extractDomain(url, { tld: true }).then(console.log);
// example.co.uk
```

Please note that using the tld flag may significantly slow down the process. Benchmark (old) results:

```
# extract domain 10,000 times
  end ~14 ms (0 s + 13572914 ns)
# extract domain with tld 10,000 times
  end ~4.29 s (4 s + 288108681 ns)
```

## Tests

```bash
$ bun test
```

## Coding style

```bash
$ bun pretty
```

## Benchmark

```bash
$ bun benchmark
```

## Contribution

Contributions are appreciated.

## License

MIT-licensed. See LICENSE.

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