ini

中文文档
7.0.0发布于 2026/05/09ISCCommonJS零依赖构建来源已验证近 30 天下载 6.8亿

用于node的ini编码器/解码器

中文使用文档
文档目录6 节
  1. 请注意
  2. 用法
  3. API
  4. 解析
  5. Stringify
  6. Un / Escape

一个INI格式解析器和序列化器。

请注意

  • section被视为嵌套对象。

  • 无节项被视为全局项。

用法

考虑如下的INI文件:

; This comment is being ignored
scope = global

[database]
user = dbuser
password = dbpassword
database = use_this_database

[paths.default]
datadir = /var/lib/data
array[] = first value
array[] = second value
array[] = third value

你可以 读, 修改 和 写 它是这样的:

import { writeFile , readFile } from 'node:fs/promises'
import { stringify , parse } from 'ini'

//  Read INI file as text

let text = await readFile(`./Original.ini`,{
    encoding : 'utf-8'
})

//  Parse text data to object

const config = parse(text)

//  Modify data object

config.scope = 'local'
config.database.database = 'use_another_database'
config.paths.default.tmpdir = '/tmp'
delete config.paths.default.datadir
config.paths.default.array.push('fourth value')

//  Stringify data object

text = stringify(config,{ 
    section : 'section' 
})

//  Write INI file as text

await writeFile(`./Modified.ini`,text)

书面文件将包括以下内容:

[section]
scope=local
[section.database]
user=dbuser
password=dbpassword
database=use_another_database
[section.paths.default]
tmpdir=/tmp
array[]=first value
array[]=second value
array[]=third value
array[]=fourth value

API

解析

尝试将给定的INI字符串转换为嵌套的数据对象。

// You can also use `decode`
const object = parse(`<INI Text>`) 

Stringify

将给定的数据对象编码为INI格式的字符串。

// You can also use `encode`
stringify(object,{

    /**
     *  Whether to insert spaces before & after `=`
     * 
     *  Disabled by default to have better 
     *  compatibility with old picky parsers.
     */

    whitespace : false ,

    /**
     *  Whether to align the `=` character for each section.
     *  -> Also enables the `whitespace` option
     */

    align : false ,

    /**
     *  Identifier to use for global items 
     *  and to prepend to all other sections.
     */

    section ,

    /**
     *  Whether to sort all sections & their keys alphabetically.
     */

    sort : false ,

    /**
     *  Whether to insert a newline after each section header.
     * 
     *  The TOSHIBA & FlashAir parser require this format. 
     */

    newline : false ,

    /**
     *  Which platforms line-endings should be used.
     * 
     *  win32 -> CR+LF
     *  other -> LF
     * 
     *  Default is the current platform
     */

    platform ,

    /**
     *  Whether to append `[]` to array keys.
     * 
     *  Some parsers treat duplicate names by themselves as arrays
     */

    bracketedArray : true

})

为向后兼容,任何作为
选项参数被视为 section 选择。

stringify(object,'section')

Un / Escape

将给定的字符串转换为安全的
在INI文件中使用key或value。

safe(`"unsafe string"`) // -> \"unsafe string\"

或者用:

unsafe(`\\"safe string\\"`) // -> "safe string"

版本与发布记录

ini 共发布 29 个版本,首次发布于 2011/08/07;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
7.0.0latest2026/05/095 个月前
6.0.02025/10/221 年前
5.0.02024/09/052 年前
4.1.32024/05/222 年前
4.1.22024/03/052 年前
4.1.12023/05/173 年前

关于 ini 的常见问题

ini 是什么?

用于node的ini编码器/解码器

如何安装 ini?

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

ini 支持 TypeScript 吗?

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

ini 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

ini 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^22.22.2 || ^24.15.0 || >=26.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

ini 可以商用吗?

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

ini 的下载量有多少?

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

ini 还在维护吗?

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