properties-file parser & editor
The `properties-file` library provides a high-performance, lossless parser, editor, and formatter for Java-style `.properties` files. Currently stable at version 5.0.4, it undergoes active maintenance with frequent releases (multiple patches per month based on recent history). A key differentiator is its lossless data model, preserving comments, blank lines, and duplicate keys, allowing for exact reconstruction or normalized output via `format()` options. It boasts 3–7x faster parsing than alternatives, zero dependencies, and a tiny bundle size (970 B min+gzip for `getProperties`). Compiled to ES5, it offers broad compatibility, running on Node.js versions back to 0.4.0 and any modern browser. It also includes bundler integrations for Webpack, Rollup/Vite, esbuild, and Bun to import `.properties` files directly.
Common errors
-
TypeError: (0 , properties_file_1.PropertiesEditor) is not a constructor
cause Attempting to import `PropertiesEditor` from the root `properties-file` module instead of its specific subpath.fixChange your import statement to `import { PropertiesEditor } from 'properties-file/editor'`. -
Module not found: Error: Can't resolve 'properties-file/webpack-loader' in '...' OR Cannot find module 'properties-file/webpack-loader'
cause Using the old Webpack loader path which was changed in v4.0.0.fixUpdate the import or configuration path for the Webpack loader to `properties-file/bundler/webpack`. -
Property 'someMethod' does not exist on type 'Properties'.
cause Attempting to use methods or properties on the `Properties` class that existed in v3.x or prior, or expecting it to behave like a simple key-value object after v5.0.0's lossless model change.fixFor basic key-value access, use `getProperties()`. For the `Properties` class (v5+), interact with its `nodes` array for granular control and use `format()` to generate output, or refer to TSDoc for available methods on the lossless data model.
Warnings
- breaking The Webpack loader export path changed from `properties-file/webpack-loader` to `properties-file/bundler/webpack`.
- breaking Version 5.0.0 introduced a significant architectural change to a lossless data model for the `Properties` class. This means every element (properties, comments, blank lines, whitespace, duplicate keys) is preserved as typed nodes. While this enables exact reconstruction and powerful transformations, code directly manipulating the internal structure of `Properties` instances or expecting a simple key-value object from `new Properties()` will break.
- gotcha When deleting duplicate keys, the default behavior of `delete()` removes the *last* occurrence. If you need to remove the first or a specific occurrence, you must explicitly use the `occurrence` option.
Install
-
npm install properties-file -
yarn add properties-file -
pnpm add properties-file
Imports
- getProperties
const { getProperties } = require('properties-file')import { getProperties } from 'properties-file' - Properties
import { Properties } from 'properties-file'import { Properties } from 'properties-file/parser' - PropertiesEditor
import { PropertiesEditor } from 'properties-file'import { PropertiesEditor } from 'properties-file/editor' - Webpack Loader
import 'properties-file/webpack-loader'
import 'properties-file/bundler/webpack'
Quickstart
import { readFileSync } from 'node:fs';
import { getProperties, Properties, PropertiesNodeType } from 'properties-file/parser';
import { PropertiesEditor } from 'properties-file/editor';
// Example .properties content
const fileContent = `
# This is a comment
hello=world
foo:bar\n\r
key.with.escapes=value with spaces and \\ backslashes
duplicate=first
duplicate=second
`;
// 1. Basic key-value parsing
const simpleObject = getProperties(fileContent);
console.log('Simple object:', simpleObject);
// Expected: { hello: 'world', 'foo:bar': '', 'key.with.escapes': 'value with spaces and \ backslashes', duplicate: 'second' }
// 2. Lossless parsing with full data model
const properties = new Properties(fileContent);
console.log('\nLossless Nodes:');
for (const node of properties.nodes) {
switch (node.type) {
case PropertiesNodeType.PROPERTY:
console.log(`PROPERTY: ${node.key} = ${node.value}`);
break;
case PropertiesNodeType.COMMENT:
console.log(`COMMENT: ${node.body}`);
break;
case PropertiesNodeType.BLANK:
console.log('BLANK LINE');
break;
}
}
// 3. Editing properties
const editor = new PropertiesEditor(fileContent);
editor.upsert('new_key', 'new_value');
editor.delete('duplicate', { occurrence: 'first' }); // Delete the first 'duplicate'
editor.update('hello', 'updated_world');
console.log('\nEdited properties:');
console.log(editor.format());