@frsource/frs-replace

中文文档
5.1.157发布于 2026/11/09MITTypeScriptES Module构建来源已验证近 30 天下载 3387

Simple wrapper around javascript replace with CLI usage support!

原始使用文档
文档目录10 节
  1. :scroll: Installation
  2. :keyboard: CLI
  3. Arguments
  4. Flags
  5. :mag_right: Examples and recipes - CLI
  6. :books: Node API
  7. :mag_right: Examples and recipes - Node
  8. :chart_with_upwards_trend: Benchmarks
  9. input as glob pattern [40 files]
  10. input & replacement as strings

NPM version badge GZIP size calculated by bundlejs.com semantic-release badge license MIT badge

@frsource/frs-replace - replace content directly in your files/streams with ease (CLI included)! 📝

File an Issue · Question or an idea? · Benchmarks

The fastest (see benchmarks) CLI & Node wrapper around javascript replace which allows in-file replacing (with possibility to save changed file as a copy), globbing, piping and many more!

Getting Started (CLI) · Examples and recipes (CLI)
Getting Started (Node) · Examples and recipes (Node)

:scroll: Installation

npm install @frsource/frs-replace

or execute it right away:

npx @frsource/frs-replace

:keyboard: CLI

frs-replace <regex> <replacement> [flags]

Basic usage:

frs-replace something anything -i foo.js -o foo_replaced.js

# all of the occurences of "something" in `foo.js` will be replaced with "anything" and saved to `foo_replaced.js`

More examples below.

Arguments

ArgumentTypeDescription
<regex>stringFirst parameter to RegExp constructor
<replacement>stringString or path to replacement function file (see ‑‑replace‑fn switch for details)

Flags

Note: Every boolean option can be negated with use of --no- prefix, e.g. --stdout or --no-stdout turn stdout output on or off, respectively.

Note: Object types can be set using dot notation. So, e.g. if you want to pass utf8 value under i-read-opts encoding field you should write --i-read-opts.encoding utf8.

OptionTypeDefaultDescription
‑i, ‑‑inputstring or string[]-Path to files or fast-glob pattern pointing to files to be read & replaced from. If multiple files specified results will be joined using outputJoinString option's value)
‑‑i-read-optsstring or objectutf8Options which are passed directly to the readFileSync method when reading input file
‑‑i-glob-optsobjectundefinedOptions which are passed directly to the fast-glob package when resolving glob patterns
‑‑stdinbooleantrueWait for stdin input (should be set to false when used in non-interactive terminals)
‑o, ‑‑outputstring-Output file name/path (replaces the file if it already exists and creates any intermediate directories if they don't already exist)
‑‑o-write-optsstring or objectutf8Passed as options argument of write's .sync
‑‑o-join-strstring\nUsed when joining multiple files, passed directly to javascript join
‑c, ‑‑contentstring-Content to be replaced (takes precedence over stream & file input)
‑s, ‑‑strategy"join" or "flatten" or "preserve-structure""join"Output file generation strategy. "join" - joins all input files and outputs them as a single file using path passed as: "output". "preserve-structure" - saves all files to the "output" directory keeping relative directory structure."flatten" - same as "preserve-structure" but flattens the directory structure
‑f, ‑‑flagsstring, combination of gim flagsgRegExp flags
‑‑stdoutbooleantrue if piped input present, false otherwiseForce sending output on stdout
‑r, ‑‑replace‑fnbooleanfalseTreat replacement argument as path to file containing replacement function
‑h, ‑‑helpboolean-Show help
‑v, ‑‑versionboolean-Show version number

:mag_right: Examples and recipes - CLI

1. Replace all a occurrences with b from the foo.js file and return the result (using CLI)
frs-replace a b -i foo.js --stdout
2. Replace all a occurrences with b from foo.js and save the result to the foo_replaced.js (using CLI)
frs-replace a b -i foo.js -o foo_replaced.js
3. Replace all a occurrences with b from an array of files and save the result to the foo_replaced.js using default \n as a result-joining string (using CLI)
frs-replace a b -i foo.js -i foo2.js -o foo_replaced.js
4. Replace all a occurrences with b from all .js files in the foo directory and save the result to the foo_replaced.js using \n/////\n as a result-joining string (using CLI)
frs-replace a b -i foo/*.js -o foo_replaced.js --o-join-str "\n/////\n"
5. Replace all a occurrences with b in a content string abcd and save the result to the foo_replaced.js (using CLI)
frs-replace a b --content abcd -o foo_replaced.js
6. Replace all a occurrences with b from a piped stream and save it to the output file (using CLI)
<read-file> | frs-replace a b > <output-file-path>
7. Replace all a occurrences with b from a piped stream and pass it through the stdout stream to the <next-command> (using CLI)
<read-file> | frs-replace a b | <next-command>

:books: Node API

@frsource/frs-replace package is shipped in 2 flavors: synchronous and asynchronous:

import { sync, async } from '@frsource/frs-replace';

sync({/* options */});
await async({/* options */});

Where /* options */ is an object containing:

Note: one of parameters: input or content is required

OptionTypeDefaultDescription
inputstring or string[]undefinedPath to files or fast-glob pattern pointing to files to be read & replaced from. If multiple files specified results will be joined using outputJoinString option's value)
inputReadOptionsstring or objectutf8Options which are passed directly to the readFileSync method when reading input file
inputGlobOptionsobjectundefinedOptions which are passed directly to the fast-glob package when resolving glob patterns
contentstringundefinedContent to be replaced (takes precedence over file input)
strategy"join" or "flatten" or "preserve-structure""join"Output file generation strategy. "join" - joins all input files and outputs them as a single file using path passed as: "output". "preserve-structure" - saves all files to the "output" directory keeping relative directory structure."flatten" - same as "preserve-structure" but flattens the directory structure
needlestring or RegExp Object-Used as a first argument of javascript replace
replacementstring-Passed as a second argument to javascript replace
outputstringundefinedPath of an output file
outputWriteOptionsstring or object"utf8"Passed as options argument of write's .sync
outputJoinStringstring\nString used when joining multiple files, passed directly to javascript join

:mag_right: Examples and recipes - Node

Note: While all of examples are using synchronous API method in all cases your can use async as well.

1. Replace all a occurrences with b from the foo.js file and return the result
import { sync } from '@frsource/frs-replace';

const result = sync({
  input: 'foo.js',
  regex: new RegExp('a', 'g'),
  replacement: 'b',
  output: 'foo_replaced.js',
});
2. Replace all a occurrences with b from foo.js and save the result to the foo_replaced.js
import { sync } from '@frsource/frs-replace';
const result = sync({
  input: 'foo.js',
  regex: new RegExp('a', 'g'),
  replacement: 'b',
  output: 'foo_replaced.js',
});
3. Replace all a occurrences with b from an array of files and save the result to the foo_replaced.js using default \n as a result-joining string
import { sync } from '@frsource/frs-replace';
const result = sync({
  input: ['foo.js', 'foo2.js'],
  regex: new RegExp('a', 'g'),
  replacement: 'b',
  output: 'foo_replaced.js',
});
4. Replace all a occurrences with b from all .js files in the foo directory and save the result to the foo_replaced.js using \n/////\n as a result-joining string
import { sync } from '@frsource/frs-replace';
const result = sync({
  input: 'foo/*.js',
  regex: new RegExp('a', 'g'),
  replacement: 'b',
  outputJoinString: '\n/////\n',
  output: 'foo_replaced.js',
});
5. Replace all a occurrences with b in a content string abcd and save the result to the foo_replaced.js
import { sync } from '@frsource/frs-replace';
const result = sync({
  content: 'abcd',
  regex: new RegExp('a', 'g'),
  replacement: 'b',
  output: 'foo_replaced.js',
});

:chart_with_upwards_trend: Benchmarks

Tested on Node v24.22.2.

input as glob pattern [40 files]

RankLibraryAverage latency [ms]Difference percentage (comparing to best average latency)
1@frsource/frs-replace (sync)0.40 ± 1.33%+0.00%
2replace-in-file (sync)0.47 ± 2.40%+16.76%
3@frsource/frs-replace (async)1.76 ± 1.58%+339.49%
4replace-in-file (async)2.42 ± 2.16%+502.18%

input & replacement as strings

RankLibraryAverage latency [ms]Difference percentage (comparing to best average latency)
1@frsource/frs-replace (sync)0.00 ± 0.70%+0.00%
2replaceString0.00 ± 0.65%+28.31%
3@frsource/frs-replace (async)0.00 ± 0.80%+38.74%

版本与发布记录

@frsource/frs-replace 共发布 174 个版本,首次发布于 2020/05/16;过去 12 个月发布了 72 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
5.1.157latest2026/11/096 天前
5.1.1562026/11/096 天前
5.1.1552026/11/0411 天前
5.1.1542026/11/0213 天前
5.1.1532026/11/0213 天前
5.1.1522026/10/2620 天前

关于 @frsource/frs-replace 的常见问题

@frsource/frs-replace 是什么?

Simple wrapper around javascript replace with CLI usage support!

如何安装 @frsource/frs-replace?

在项目目录运行 npm install @frsource/frs-replace,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 frs-replace,可以用 npx frs-replace 直接运行。

@frsource/frs-replace 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@frsource/frs-replace 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@frsource/frs-replace 需要什么版本的 Node.js?

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

@frsource/frs-replace 可以商用吗?

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

@frsource/frs-replace 的下载量有多少?

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

@frsource/frs-replace 还在维护吗?

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