replace-in-file
Version:
A simple utility to quickly replace text in one or more files.
75 lines (57 loc) • 1.89 kB
Markdown
[](https://www.npmjs.com/package/replace-in-file)
[](https://david-dm.org/adambuczynski/replace-in-file)
[](https://travis-ci.org/adambuczynski/replace-in-file)
[](https://coveralls.io/github/adambuczynski/replace-in-file?branch=master)
[](https://github.com/adambuczynski/replace-in-file/issues)
A simple utility to quickly replace text in one or more files.
```shell
npm install replace-in-file
```
```js
var replace = require('replace-in-file');
//Asynchronous
replace({
//Single file
files: 'path/to/file',
//Or multiple files
files: [
'path/to/file',
'path/to/other/file',
],
//Replacement to make (can be string or regex)
replace: /Find me/g,
with: 'Replacement'
}, function(error, changedFiles) {
//Catch errors
if (error) {
return console.error('Error occurred:', error);
}
//List changed files
console.log('Modified files:', changedFiles.join(', '));
});
//Synchronous
try {
var changedFiles = replace({
//Single file
files: 'path/to/file',
//Or multiple files
files: [
'path/to/file',
'path/to/other/file',
],
//Replacement to make (can be string or regex)
replace: /Find me/g,
with: 'Replacement'
});
console.log('Modified files:', changedFiles.join(', '));
}
catch (error) {
console.error('Error occurred:', error);
}
```
(MIT License)
Copyright 2015-2016, [Adam Buczynski](http://adambuczynski.com)