What is replace-in-file?
The replace-in-file npm package allows you to easily search and replace text in files. It is useful for tasks such as updating configuration files, modifying source code, and performing batch text replacements.
What are replace-in-file's main functionalities?
Basic Replacement
This feature allows you to perform a basic search and replace operation in a specified file. The 'from' field can be a string or a regular expression, and the 'to' field is the replacement string.
const replace = require('replace-in-file');
const options = {
files: 'path/to/file.txt',
from: /oldText/g,
to: 'newText',
};
replace(options)
.then(results => console.log('Replacement results:', results))
.catch(error => console.error('Error occurred:', error));
Multiple Replacements
This feature allows you to perform multiple search and replace operations in a single file. The 'from' and 'to' fields are arrays, where each element in 'from' is replaced by the corresponding element in 'to'.
const replace = require('replace-in-file');
const options = {
files: 'path/to/file.txt',
from: [/oldText1/g, /oldText2/g],
to: ['newText1', 'newText2'],
};
replace(options)
.then(results => console.log('Replacement results:', results))
.catch(error => console.error('Error occurred:', error));
Glob Pattern Matching
This feature allows you to use glob patterns to specify multiple files for the search and replace operation. This is useful for batch processing multiple files in a directory.
const replace = require('replace-in-file');
const options = {
files: 'path/to/*.txt',
from: /oldText/g,
to: 'newText',
};
replace(options)
.then(results => console.log('Replacement results:', results))
.catch(error => console.error('Error occurred:', error));
Dry Run
This feature allows you to perform a dry run of the search and replace operation. No actual changes are made to the files, but you can see what the results would be. This is useful for testing your replacement logic before applying it.
const replace = require('replace-in-file');
const options = {
files: 'path/to/file.txt',
from: /oldText/g,
to: 'newText',
dry: true,
};
replace(options)
.then(results => console.log('Dry run results:', results))
.catch(error => console.error('Error occurred:', error));
Other packages similar to replace-in-file
replace
The 'replace' package is another tool for performing search and replace operations in files. It offers similar functionality to 'replace-in-file' but with a simpler API. It is suitable for straightforward replacement tasks.
string-replace-loader
The 'string-replace-loader' package is a webpack loader that allows you to perform string replacements in your source files during the build process. It is useful for replacing text in JavaScript and other files as part of your build pipeline.
gulp-replace
The 'gulp-replace' package is a plugin for the Gulp task runner that allows you to perform search and replace operations in your Gulp streams. It integrates well with Gulp workflows and is useful for automating text replacements in your build process.
Replace in file

A simple utility to quickly replace text in one or more files.
Installation
npm install replace-in-file
Usage
var replace = require('replace-in-file');
replace({
files: 'path/to/file',
files: [
'path/to/file',
'path/to/other/file',
],
replace: /Find me/g,
with: 'Replacement'
}, function(error, changedFiles) {
if (error) {
return console.error('Error occurred:', error);
}
console.log('Modified files:', changedFiles.join(', '));
});
try {
var changedFiles = replace({
files: 'path/to/file',
files: [
'path/to/file',
'path/to/other/file',
],
replace: /Find me/g,
with: 'Replacement'
});
console.log('Modified files:', changedFiles.join(', '));
}
catch (error) {
console.error('Error occurred:', error);
}
License
(MIT License)
Copyright 2015-2016, Adam Buczynski