Socket
Book a DemoInstallSign in
Socket

replace-in-file

Package Overview
Dependencies
Maintainers
1
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

replace-in-file

A simple utility to quickly replace text in one or more files.

Source
npmnpm
Version
2.0.2
Version published
Weekly downloads
1.4M
0.64%
Maintainers
1
Weekly downloads
 
Created
Source

Replace in file

npm version node dependencies build status coverage status github issues

A simple utility to quickly replace text in one or more files or globs. Works synchronously or asynchronously with either promises or callbacks.

Installation

npm install replace-in-file

Usage

Specify options:

const replace = require('replace-in-file');
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)
  replace: /Find me/g,
  with: 'Replacement',

  //Specify if empty/invalid file paths are allowed, defaults to false.
  //If set to true these paths will fail silently and no error will be thrown.
  allowEmptyPaths: false,
};

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);
}

License

(MIT License)

Copyright 2015-2016, Adam Buczynski

Keywords

replace

FAQs

Package last updated on 30 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts