read-cfg-file
v1.0.1
Published
A lightweight utility for reading and parsing configuration files in multiple formats.
Readme
read-cfg-file
A lightweight utility for reading and parsing configuration files in multiple formats.
Overview
read-cfg-file is a versatile tool that supports reading and parsing configuration files in various formats including .js, .cjs, .ts, .mts, .mjs, .json, .jsonc, .json5, .yaml, and .yml. It leverages modern build tools like Vite to dynamically compile TypeScript files and provides a unified interface to return the parsed configuration content.
Features
- Supports multiple configuration file formats.
- Automatically handles relative and absolute paths.
- Built-in support for TypeScript using Vite to compile into temporary
.mjsfiles before loading. - Cleans up generated temporary files to keep the environment tidy.
- Exception-safe handling to ensure recovery as much as possible on failure.
Documentation
For detailed usage instructions and API references, please visit the official documentation:
Quick Start
Basic Usage
import { readCfgFile } from 'read-cfg-file';
// Reading a JSON file
const jsonConfig = await readCfgFile<{ port: number }>('./config.json');
console.log(jsonConfig?.port); // Outputs port number
// Reading a YAML file
const yamlConfig = await readCfgFile<{ db: string }>('./config.yaml');
console.log(yamlConfig?.db); // Outputs database nameContributing
We welcome contributions from the community! If you find a bug or want to suggest an improvement, feel free to open an issue or submit a pull request.
How to Contribute
- Fork the repository.
- Create a new branch for your changes.
- Submit a pull request with a clear description of your changes.
License
This project is licensed under the MIT License.

