npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

hosts-utilities

v0.1.0

Published

Utilities for the hosts file

Readme

Hosts Utilities - Beta

Table of Contents

What is it?

A few node utilities for working with your hosts file, inspired by Hostile

[!note] If you're looking for a CLI frontend, raise an issue and I'll write one.

Why did I make this library?

I wanted to programatically work with my hosts file while maintaining formatting. Hostile doesn't seem to be maintained anymore and lacks features I was looking for.

Why is it in beta?

Because I haven't worked much with hosts files and can't be confident in the code until it's used in the real world.

The hosts file seems simple, but in my experience, real-world implementations of anything are quirky.

Install

npm install hosts-utilities

Use

Assume a hosts file /etc/hosts

1.2.3.4 hostname1

We can add hostname2 using assignHostnames

import * as hosts from 'hosts-utilities'

await hosts.assignHostnames('1.2.3.4', ['hostname2'])

// updates /etc/hosts to
// 1.2.3.4 hostname1 hostname2

API

[!note] Both named and sub-path exports are available. Paths are kebab-cased. Example:

import { hostsPath } from 'hosts-utilities'
// or
import hostsPath from 'hosts-utilities/hosts-path'

assignHostnames

What is it?

  • A function to add or update host entries

Why use it?

  • To add hostnames to an existing entry, or add a new entry

Signature

  • async (ip: string, hostnames: string[], options: AssignOptions = {}) => undefined
    • ip and hostnames must match /^[^#\s]+$/

Examples

Assume a file /etc/hosts with

1.2.3.4 hostname1 hostname2
await assignHostnames('1.2.3.4', ['hostname1', 'hostname3'])
// updates /etc/hosts to
// 1.2.3.4 hostname1 hostname2 hostname3

await assignHostnames('5.6.7.8', ['hostname4'])
// adds the line
// 5.6.7.8 hostname4

hostsPath

A string

  • On windows: C:\Windows\System32\drivers\etc\hosts
  • On not windows: /etc/hosts

parse

What is it?

  • A function which parses the hosts file

Why use it?

  • This let's you perform your own logic on the hosts file. For example, you could parse it, modify the parsed lines, then call write(updatedParsedLines)

Signature

Examples

const parsedLines1 = await parse()
const parsedLines2 = await parse({ filePath: '/path/to/custom/hosts' })

removeHostnames

What is it?

  • A function removing the hostnames from a hosts file

Why use it?

  • Self explanatory

Signature

  • async (hostnames: string[], options: RemoveOptions = {}) => undefined
    • hostnames must match /^[^#\s]+$/

Examples

Let's assume a file /etc/hosts with

1.2.3.4 hostname1
5.6.7.8 hostname2 hostname3
await removeHostnames(['hostname1', 'hostname2'])
// updates /etc/hosts to
// 5.6.7.8 hostname3

write

What is it?

  • A function to write parsedLines to the host file

Why use it?

  • Like parse, this lets you perform your own logic on the hosts file. For example, you could parse it, modify the parsedLines, then write them.

Signature

Examples

const parsedLines = await parse()
const updatedLines = doSomethingTo(parsedLines)
await write(updatedLines)

Schemas

Parse Options

{
  // default: hostsPath
  // validation: minimum of one character
  filePath?: string
}

Remove Hostnames Options

Includes FormatOptions

FormatOptions & {
  // default: hostsPath
  // validation: minimum of one character
  filePath?: string

  // default: undefined (removes hostnames for all ips)
  // validation: matches /^[^#\s]+$/
  withIp?: string
}

[!warning] The option withIp is provided because Hostile supports it, meaning I assume someone found it helpful. Keep in mind hosts shouldn't be mapped to more than one IP address, so withIp should be unnecessary.

Assign Hostnames Options

Includes FormatOptions

FormatOptions & {
  // default: hostsPath
  // validation: minimum of one character
  filePath?: string
}

Write Options

Includes FormatOptions

FormatOptions & {
  // default: hostsPath
  // validation: minimum of one character
  filePath?: string
}

Format Options

[!note] Due to complexity, these options may not behave as you expect.

At a glance

{
  preserveFormatting?: boolean = true
  separatorParts?: string = '\t'
  separatorHostname?: string = ' '
}

Parsed Line

[!note] Due to complexity, this structure may not work how you expect.

At a glance

{
  original: string
  data: {
    ip: string
    hostnamesWithSpace: string[]
    comment: string
    space: {
      beforeIp: string
      afterIp: string
      beforeComment: string
    }
  }
}