@fnet/config

中文文档
0.2.33发布于 2026/05/21MITESM + CJS近 30 天下载 2436
原始使用文档
文档目录13 节
  1. How It Works
  2. Key Features
  3. Conclusion
  4. Overview
  5. Installation
  6. Usage
  7. Examples
  8. Example 1: Loading a Single Configuration File
  9. Example 2: Loading Multiple Configuration Files
  10. Example 3: Handling Optional Configurations
  11. Configuration File Search
  12. Return Value Structure
  13. Acknowledgement

@fnet/config

The @fnet/config module is a utility designed to simplify the process of managing and accessing configuration files for applications. By focusing on ease of use and flexibility, it helps users retrieve configuration settings from YAML files located in designated directories. It caters to both single and batch processing of configuration files and allows users to work with custom environmental variables seamlessly.

How It Works

The module scans specified directories to locate YAML configuration files that match the provided name(s). Users can specify a default directory, relative paths, or even use the current working directory. Once a configuration is found, the module parses it and optionally updates the system's environmental variables based on the file's content.

Key Features

  • Directory Searching: Automatically searches in a sequence of specified directories and relative paths for configuration files.
  • YAML Parsing: Processes YAML files and extracts their data for easy access.
  • Environmental Variable Integration: Optionally injects configuration settings into the system’s environment variables.
  • Batch Processing: Supports handling multiple configuration files at once by accepting arrays of names.
  • Tag Filtering: Allows selective processing of YAML content based on specified tags.
  • Optional File Presence: Users can specify whether the absence of a configuration file should trigger an error.

Conclusion

The @fnet/config module provides a straightforward way to manage configuration files, making it easier for users to organize and access application settings. With its uncomplicated setup and useful features, it offers a practical solution for managing application configurations without unnecessary complexity.

@fnet/config Developer Guide

Overview

The @fnet/config library simplifies the process of loading and managing configuration files in your applications. It is particularly useful for environments where configurations need to be dynamic and flexible. The library allows developers to effortlessly locate configuration files across specified directories and optionally transfer configuration-defined environment variables to process.env.

Installation

To use @fnet/config in your project, you can install it via npm or yarn with the following commands:

npm install @fnet/config
yarn add @fnet/config

Usage

The library provides a default asynchronous function designed to locate and process configuration files, handling various extensions such as .fnet, .fnet.yaml, and .fnet.yml. Below is a step-by-step guide on how to use the library.

import loadConfig from '@fnet/config';

async function loadMyConfig() {
  try {
    const config = await loadConfig({
      name: 'myConfig',                    // Name of the config file without extension
      dir: ['path/to/config', 'another/path'], // Directories to search
      rel: ['./.fnet/'],                   // Relative paths within directories
      transferEnv: true,                   // Transfer env variables to process.env
      optional: false,                     // Throws error if file not found
      tags: ['production']                 // Filter by specific tags in YAML
    });

    console.log('Configuration file:', config.file);
    console.log('Parsed data:', config.data);
    console.log('Raw content:', config.raw);
  } catch (error) {
    console.error('Error loading configuration:', error);
  }
}

loadMyConfig();

Examples

Below are examples demonstrating typical usage scenarios for @fnet/config.

Example 1: Loading a Single Configuration File

import loadConfig from '@fnet/config';

async function loadSingleConfig() {
  const config = await loadConfig({ name: 'app-config' });
  console.log(config.file);  // Absolute path to the found file
  console.log(config.data);  // Parsed configuration data
}

Example 2: Loading Multiple Configuration Files

import loadConfig from '@fnet/config';

async function loadMultipleConfigs() {
  const configs = await loadConfig({
    name: ['db-config', 'api-config'],
    dir: process.cwd()
  });
  
  configs.forEach(config => {
    console.log('File:', config.file);
    console.log('Data:', config.data);
  });
}

Example 3: Handling Optional Configurations

import loadConfig from '@fnet/config';

async function loadOptionalConfig() {
  try {
    const config = await loadConfig({
      name: 'optional-config',
      optional: true  // Returns null if file not found
    });
    
    if (config) {
      console.log('Configuration loaded:', config.data);
    } else {
      console.log('Configuration file not found');
    }
  } catch (e) {
    console.log('Unexpected error:', e.message);
  }
}

The library searches for configuration files in the following order:

  1. In specified directories (or process.cwd() if not specified)
  2. Through specified relative paths (defaults to ./.fnet/, ../.fnet/, ../../.fnet/, ../../../.fnet/)
  3. With supported extensions: .fnet, .fnet.yaml, .fnet.yml

Return Value Structure

The function returns an object (or array of objects) with the following properties:

  • file: Absolute path to the found configuration file
  • data: Parsed configuration data
  • config: Alias for data (deprecated)
  • raw: Raw YAML content

Acknowledgement

The @fnet/config library uses @fnet/yaml for parsing YAML files, ensuring efficient and reliable handling of configuration files.

Input Schema

$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
  name:
    oneOf:
      - type: string
      - type: array
        items:
          type: string
    description: The name of the configuration or an array of names.
  dir:
    oneOf:
      - type: string
      - type: array
        items:
          type: string
    description: The directories to search within. If not specified, uses process.cwd()
  rel:
    type: array
    items:
      type: string
    default:
      - ./.fnet/
      - ../.fnet/
      - ../../.fnet/
      - ../../../.fnet/
    description: Relative paths to consider within directories, will be normalized.
  transferEnv:
    type: boolean
    default: true
    description: Whether to transfer 'env' values from the configuration to process.env.
  optional:
    type: boolean
    default: false
    description: Whether the presence of the file is optional. If false, throws an
      error when file isn't found.
  tags:
    type: array
    items:
      type: string
    default: []
    description: An array of tags used for filtering YAML content.
required:
  - name
additionalProperties: false
description: Schema for the configuration file loader input options.

Output Schema

$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
  file:
    type: string
    description: Absolute path to the found configuration file
  data:
    type: object
    description: Parsed configuration data
  config:
    type: object
    description: Alias for data (deprecated)
    deprecated: true
  raw:
    type: string
    description: Raw YAML content
required:
  - file
  - data
  - config
  - raw
additionalProperties: false
description: Schema for the configuration file loader output format

版本与发布记录

@fnet/config 共发布 39 个版本,首次发布于 2023/09/29;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.2.33latest2026/05/215 个月前
0.2.322026/03/018 个月前
0.2.312025/10/231.1 年前
0.2.302025/10/231.1 年前
0.2.292025/10/161.1 年前
0.2.282025/04/111.6 年前

关于 @fnet/config 的常见问题

如何安装 @fnet/config?

在项目目录运行 npm install @fnet/config,或使用 pnpm add / yarn add / bun add。

@fnet/config 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/fnet__config 是否存在。

@fnet/config 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fnet/config 可以商用吗?

@fnet/config 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fnet/config 的下载量有多少?

根据 npm 官方下载统计,@fnet/config 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,436 次,日均约 81 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fnet/config 还在维护吗?

最新版本 0.2.33 发布于 2026/05/21(5 个月前);至今共发布 39 个版本,其中过去 12 个月发布了 2 个;首次发布于 2023/09/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。