Replace In File is a JavaScript/TypeScript utility for programmatic and CLI-based text replacement within one or more files. Currently at version 8.4.0, it provides both asynchronous (Promise-based) and synchronous APIs, supporting glob patterns for file selection. The library facilitates simple string replacements, complex regular expression substitutions, and handling of multiple replacements with distinct options. Its release cadence includes major version bumps that introduce breaking changes, notably affecting the return value structure. Key differentiators include its flexibility in handling various replacement scenarios, support for custom file system APIs, and a dry-run mode, making it suitable for build scripts, content transformation, and general file manipulation tasks in Node.js environments.
npm install replace-in-fileVerified import paths — ran on the pinned version, not inferred.
Demonstrates asynchronous text replacement in a file using a regular expression, including file creation and cleanup.
Update existing code to process the new results array. To get an array of changed files, use `results.filter(r => r.hasChanged).map(r => r.file)`.
Upgrade your Node.js environment to version 18 or newer to ensure compatibility.
If experiencing issues with CLI config paths, change absolute paths to relative paths by removing any leading slashes, or ensure absolute paths are correctly specified relative to the root.
Prefer using the `verbose` option (set to `false` for no output) instead of `silent`.
Ensure your project is configured for ESM (`"type": "module"` in `package.json` or `.mjs` file extension) and use `import { replaceInFile } from 'replace-in-file';`Verify the file paths and glob patterns specified in the `files` option. Ensure the target files exist and the pattern correctly resolves to them.
Check the `from` option in your configuration. It must be a `string`, a `RegExp` object, or an `Array` of `string`s or `RegExp`s.
No dependency data recorded yet.