@fnet/config

中文文档
0.2.33发布于 2026/05/21MITESM + CJS近 30 天下载 2436
中文使用文档
文档目录13 节
  1. 如何运作
  2. 关键特性
  3. 结论
  4. 概览
  5. 安装
  6. 使用量
  7. 实例
  8. 例1: 装入单个配置文件
  9. 示例2: 装入多个配置文件
  10. 例3:处理可选配置
  11. 配置文件搜索
  12. 返回值结构
  13. 致谢

@ fnet/ 配置

这个 @fnet/config 模块是一种用于简化应用程序配置文件的管理和访问程序的功能。 它通过注重易用性和灵活性,帮助用户从位于指定目录的YAML文件中检索配置设置. 它既满足了对配置文件的单一和分批处理,也允许用户无缝地与自定义的环境变量合作.

如何运作

模块扫描指定目录以定位匹配所提供名称的YAML配置文件. 用户可以指定默认目录,相对路径,甚至使用当前工作目录. 一旦找到一个配置,模块会解析它,并根据文件的内容可选择地更新系统的环境变量.

关键特性

  • 目录搜索: 在指定目录和配置文件相对路径的序列中自动搜索.
  • YAML 解析: 处理YAML文件并提取其数据以便于访问.
  • 环境可变一体化:可选地将配置设置注入系统的环境变量.
  • 批处理: 支持通过接受数组名称同时处理多个配置文件.
  • 标记过滤:允许根据指定的标记对YAML内容进行选择性处理.
  • 可选文件存在:用户可以指定缺少配置文件是否触发出错.

结论

这个 @fnet/config 模块提供了管理配置文件的直截了当的方法,使用户更容易组织和访问应用程序设置. 由于其不复杂的设置和有用的功能,它为管理应用程序配置提供了实用的解决方案,而不需要复杂.

@ fnet/ 配置开发者指南

概览

这个 @fnet/config 库简化了应用程序中配置文件的加载和管理程序。 对于配置需要动态和灵活的环境特别有用。 库允许开发者在指定的目录中无所顾忌地定位配置文件,并可选择将配置定义的环境变量传输到 process.env。 。 。 .

安装

用于 @fnet/config 在您的工程中,您可以通过 npm 或 纱线安装它,其命令如下:

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

使用量

库提供了一个默认的同步函数,旨在定位和处理配置文件,处理各种扩展,例如 .fnet, (中文(简体) ), .fnet.yaml,以及 .fnet.yml。下面是关于如何使用图书馆的分步骤指南.

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();

实例

以下是一些实例,说明典型的使用情景: @fnet/config。 。 。 .

例1: 装入单个配置文件

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
}

示例2: 装入多个配置文件

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);
  });
}

例3:处理可选配置

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);
  }
}

库搜索配置文件的顺序如下:

  1. 在指定的目录(或 process.cwd() 如果未指明)
  2. 通过指定的相对路径(默认为 ./.fnet/, (中文(简体) ), ../.fnet/, (中文(简体) ), ../../.fnet/, (中文(简体) ), ../../../.fnet/(中文(简体) )
  3. 有支持的扩展 : .fnet, (中文(简体) ), .fnet.yaml, (中文(简体) ), .fnet.yml

返回值结构

函数返回具有以下属性的对象(或对象数组):

  • file: 找到配置文件的绝对路径
  • data: 解析配置数据
  • config: 数据别名(过期)
  • raw: Raw YAML 内容

致谢

这个 @fnet/config 库用途 @fnet/yaml 用于解析 YAML 文件,确保高效和可靠地处理配置文件.

输入外观

$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.

输出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 和提交记录判断。