Since the replacement did not work for me, I created a simple package replacement in the npm file to quickly replace the text in one or more files. This is partly based on @asgoth's answer.
Edit (October 3, 2016) : The package now supports promises and globes, and usage instructions have been updated to reflect this.
Editing (March 16, 2018) . The package currently contains over 100,000 downloads per month and has been expanded with additional features as well as the CLI tool.
Installation:
npm install replace-in-file
Module required
const replace = require('replace-in-file');
Specify replacement options
const options = { //Single file files: 'path/to/file', //Multiple files files: [ 'path/to/file', 'path/to/other/file', ], //Glob(s) files: [ 'path/to/files/*.html', 'another/**/*.path', ], //Replacement to make (string or regex) from: /Find me/g, to: 'Replacement', };
Asynchronous replacement with promises:
replace(options) .then(changedFiles => { console.log('Modified files:', changedFiles.join(', ')); }) .catch(error => { console.error('Error occurred:', error); });
Asynchronous replacement with callback:
replace(options, (error, changedFiles) => { if (error) { return console.error('Error occurred:', error); } console.log('Modified files:', changedFiles.join(', ')); });
Synchronous Replacement:
try { let changedFiles = replace.sync(options); console.log('Modified files:', changedFiles.join(', ')); } catch (error) { console.error('Error occurred:', error); }