# @fnet/config 中文文档

规范地址：https://npmcn.com/package/%40fnet/config/

版本：0\.2\.33
npm 元数据更新时间：2026-05-21T14:51:42.760Z
中文译文更新时间：2026-11-15T03:26:00.949Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40fnet/config
原文对照：https://npmcn.com/package/%40fnet/config/?lang=original
源码仓库：https://gitlab.com/fnetai/config

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：17.5 kB，6 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：未声明
- 运行依赖：1 个
- 首次发布：2023-09-29T00:19:50.232Z
- 最新版本发布：2026-05-21T14:51:42.484Z
- 近 30 天下载量：2436（2026-10-15 至 2026-11-13，npm 官方下载统计）
- 已发布版本数：39（近 12 个月 2 个）

## 中文简介

暂无中文简介

## 安装

```sh
npm install @fnet/config
```

也可以使用：`pnpm add @fnet/config`、`yarn add @fnet/config`、`bun add @fnet/config`

## README

# @ fnet/ 配置

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

## 如何运作

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

## 关键特性

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

## 结论

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

# @ fnet/ 配置开发者指南

## 概览

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

## 安装

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

```bash
npm install @fnet/config
```

```bash
yarn add @fnet/config
```

## 使用量

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

```javascript
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: 装入单个配置文件

```javascript
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: 装入多个配置文件

```javascript
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:处理可选配置

```javascript
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 文件,确保高效和可靠地处理配置文件.

# 输入外观

```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

```yaml
$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？

在项目目录运行 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 和提交记录判断。

