Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
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.

  • 8.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
930K
decreased by-18.41%
Maintainers
1
Weekly downloads
 
Created

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

Keywords

FAQs

Package last updated on 17 Dec 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc