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

systemd-parser

v2.1.1

Published

Systemd configuration file parser

Readme

Systemd parser

Systemd configuration file parser preserving comments and structure. Windows files with CRLF line endings are not supported. Systemd ignores trailing whitespace so \r will not often be an issue, except with values spanning multiple lines, where every line must be escaped with a backslash at the end, which does not work if there's a carriage return before it.

Usage and documentation

  • parse(input, options):

Parses the input string to return an object containing all sections with keys and values. options is an object with options shown in the example below.

import { parse } from "systemd-parser"

const input = "[Section]\nKey=Value"
const output = parse(input, {
  // Function that parses input text values to any data type for the output.
  // When it doesn't return anything, values return like in the document
  parseFunc: (section: string, key: string, value: string) => {};
  // Function that serializes those values back into text strings.
  // When it doesn't return anything, the parser tries to input the value as a string
  serializeFunc: (section: string, key: string, value: {}) => string | undefined;

  warnFunc: (
    message: string,
    severity: Severity, // "hint" | "info" | "warning" | "error",
    location: [number, number, number] // line number (1-based), from char, to char
  ) => {}; // Ran on warnings

  logWarns: false;  // Log parsing warnings to the console
  strict: false;  // Make parsing fail on wrong syntax

  includePrefixed: false; // Include keys prefixed with X-
  allowedKeys: { Section: [ "Key1", "Key2" ] }; // Object of allowed sections and keys.
  includeDisallowed: false; // Include keys not allowed by allowedKeys
})

// output: { "Section": { "Key": [ "Value" ] } }
  • generate(input):

Generates a systemd file from the input object containing all sections with keys and values.

import { generate } from "systemd-parser";

const input = { Section: { Key: ["Value"] } };
const output = generate(input);
// [Section] Key=Value
  • parseDocument(input):

Parses the input string to return a modifiable Document class with metadata like parser warnings. options is an object with options shown in the example below.

The output containing all sections with keys and values is accessed with doc.output, the string containing the modified file is accessed with doc.content

  • doc.add(seection, key, value, index?): Add an assignment (key=value) to a section, optionally after the indexth assignment. When index is undefined or below 0, it's ignored
  • doc.set(section, key, value, index?): Replace assignment at index with different assignment. When index is undefined or below 0, it removes all existing assignments.
  • doc.remove(section, key, index?, strict=true): Remove assignment at index. When index is undefined or below 0, it removes all instances. When strict is set to false, it will not throw on indexes out of range
import { parseDocument } from "systemd-parser";

const options = {}; // see the first example for all options
const doc = parseDocument("[Section]\nKey=Value", options);
// doc.output: { "Section": { "Key": [ "Value" ] } }

doc.add("Section", "Key", "ThirdValue");
doc.add("Section", "Key", "SecondValue", 1); // insert
// doc.content: [Section] Key=Value Key=SecondValue Key=ThirdValue

doc.set("Section", "Key", "OtherValue", 1);
// doc.content: [Section] Key=Value Key=OtherValue Key=ThirdValue

doc.remove("Section", "Key", 1);
// doc.content: [Section] Key=Value Key=ThirdValue

doc.set("Section", "Key", "Value"); // resets all instances
// doc.content: [Section] Key=Value

doc.remove("Setion", "Key", "Value"); // removes all instances
// doc.content: ""

The parser also lets you reuse a doc object for an interactive editor, by automatically reparsing upon setting doc.content. For example:

import { parseDocument, generate } from "systemd-parser";

const doc = parseDocument(generate({ Section: { Key: ["Value1"] } }));
// doc.content: [Section] Key=Value1
// doc.output: { "Section": { "Key": [ "Value1" ] } }

doc.content = generate({ Section: { Key: ["Value2"] } });
// doc.content: [Section] Key=Value2
// doc.output: { "Section": { "Key": [ "Value2" ] } }

Systemd ini format

Basics of the file format are listed at systemd.syntax(7). For more, see the references below.

References


License

This project uses the MIT license. It uses some of the logic from Red Hat for parsing systemd ini files, which is licensed under both LGPL-2.1+ [1] and Apache-2.0 [2]. For more details, see NOTICE