Installations
npm install replace-in-files
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=12.0.0
Node Version
14.12.0
NPM Version
6.14.8
Score
85.8
Supply Chain
99.5
Quality
74.7
Maintenance
100
Vulnerability
99.6
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
wj42ftns
Download Statistics
Total Downloads
1,913,776
Last Day
408
Last Week
7,188
Last Month
41,946
Last Year
486,484
GitHub Statistics
24 Stars
163 Commits
5 Forks
2 Watching
14 Branches
4 Contributors
Package Meta Information
Latest Version
3.0.0
Package Id
replace-in-files@3.0.0
Size
9.38 kB
NPM Version
6.14.8
Node Version
14.12.0
Publised On
26 Sept 2020
Total Downloads
Cumulative downloads
Total Downloads
1,913,776
Last day
-80.4%
408
Compared to previous day
Last week
-26.6%
7,188
Compared to previous week
Last month
6%
41,946
Compared to previous month
Last year
-6.6%
486,484
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
replace-in-files
Replace text in one or more files or globs. Works asynchronously with promises.
Installation
1# Using npm 2npm install replace-in-files 3 4# Using yarn 5yarn add replace-in-files
Usage
Specify options
1const replaceInFiles = require('replace-in-files'); 2 3const options = { 4 // See more: https://www.npmjs.com/package/globby 5 // Single file or glob 6 files: 'path/to/file', 7 // Multiple files or globs 8 files: [ 9 'path/to/file', 10 'path/to/other/file', 11 'path/to/files/*.html', 12 'another/**/*.path', 13 ], 14 15 16 // See more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace 17 // Replacement 18 from: /foo/g, // string or regex 19 to: 'bar', // string or fn (fn: carrying last argument - path to replaced file) 20 21 22 // See more: https://www.npmjs.com/package/glob 23 optionsForFiles: { // default 24 "ignore": [ 25 "**/node_modules/**" 26 ] 27 } 28 29 30 // format: `${fileName}-${year}-${month}-${day}_${hour}:${minute}:${second}.{fileExtension}` 31 // fileName-2017-11-01_21:29:55.js 32 // date of createFile old file or last modificate (if not find create date) 33 saveOldFile: false // default 34 35 36 //Character encoding for reading/writing files 37 encoding: 'utf8', // default 38 39 40 shouldSkipBinaryFiles: true, // default 41 onlyFindPathsWithoutReplace: false // default 42 returnPaths: true // default 43 returnCountOfMatchesByPaths: true // default 44};
Replacing multiple occurrences
Please note that the value specified in the from
parameter is passed straight to the native String replace method. As such, if you pass a string as the from
parameter, it will only replace the first occurrence.
To replace multiple occurrences at once, you must use a regular expression for the from
parameter with the global flag enabled, e.g. /foo/g
.
Asynchronous replacement with promises
1const replaceInFiles = require('replace-in-files'); 2 3// ... 4 5replaceInFiles(options) 6 .then(({ changedFiles, countOfMatchesByPaths }) => { 7 console.log('Modified files:', changedFiles); 8 console.log('Count of matches by paths:', countOfMatchesByPaths); 9 console.log('was called with:', options); 10 }) 11 .catch(error => { 12 console.error('Error occurred:', error); 13 });
Asynchronous replacement with co yield
1const replaceInFiles = require('replace-in-files'); 2const co = require('co'); 3 4// ... 5 6co(function* () { 7 const { 8 changedFiles, 9 countOfMatchesByPaths, 10 replaceInFilesOptions 11 } = yield replaceInFiles(options); 12 console.log('Modified files:', changedFiles); 13 console.log('Count of matches by paths:', countOfMatchesByPaths); 14 console.log('was called with:', replaceInFilesOptions); 15}).catch((error) => { 16 console.log('Error occurred:', error); 17});
Asynchronous replacement with async await (node 8+)
1const replaceInFiles = require('replace-in-files'); 2 3// ... 4 5async function main() { 6 try { 7 const { 8 changedFiles, 9 countOfMatchesByPaths, 10 replaceInFilesOptions 11 } = await replaceInFiles(options); 12 console.log('Modified files:', changedFiles); 13 console.log('Count of matches by paths:', countOfMatchesByPaths); 14 console.log('was called with:', replaceInFilesOptions); 15 } catch (error) { 16 console.log('Error occurred:', error); 17 } 18} 19 20main();
Sequentially replacement
use .pipe - will be replaced with only files found at first replacement
.pipe supported only: { from, to } (the other options will be received from options in the first replacement)
1const replaceInFiles = require('replace-in-files'); 2 3// ... 4 5async function main() { 6 try { 7 const { 8 changedFiles, 9 countOfMatchesByPaths, 10 replaceInFilesOptions 11 } = await replaceInFiles(options) 12 .pipe({ from: 'foo', to: 'bar' }) 13 .pipe({ from: 'first', to: 'second' }) 14 .pipe({ from: /const/g, to: () => 'var' }); 15 console.log('Modified files:', changedFiles); 16 console.log('Count of matches by paths:', countOfMatchesByPaths); 17 console.log('was called with:', replaceInFilesOptions); 18 } catch (error) { 19 console.log('Error occurred:', error); 20 } 21} 22 23main();
Return value
The return value of the library is an object with: countOfMatchesByPaths and paths
For example:
1const replaceInFiles = require('replace-in-files'); 2 3const data = replaceInFiles({ 4 files: 'path/to/files/*.html', 5 from: 'a', 6 to: 'b', 7}); 8 9// data could like: 10{ 11 countOfMatchesByPaths: [ 12 { 13 'path/to/files/file1.html': 5, 14 'path/to/files/file3.html': 1, 15 'path/to/files/file5.html': 3 16 } 17 ], 18 paths: [ 19 'path/to/files/file1.html', 20 'path/to/files/file3.html', 21 'path/to/files/file5.html', 22 ], 23 replaceInFilesOptions: [ 24 { 25 files: 'path/to/files/*.html', 26 from: 'a', 27 to: 'b', 28 } 29 ] 30} 31 32// if empty: 33{ 34 countOfMatchesByPaths: [ 35 {} 36 ], 37 paths: [] 38} 39 40// if used 2 .pipe 41{ 42 countOfMatchesByPaths: [ 43 { 44 'path/to/files/file1.html': 5, 45 'path/to/files/file3.html': 1, 46 'path/to/files/file5.html': 3 47 }, 48 { 49 'path/to/files/file5.html': 4 50 }, 51 { 52 'path/to/files/file1.html': 2, 53 'path/to/files/file5.html': 4 54 } 55 ], 56 paths: [ 57 'path/to/files/file1.html', 58 'path/to/files/file3.html', 59 'path/to/files/file5.html', 60 ], 61 replaceInFilesOptions: [ 62 { 63 files: 'path/to/files/*.html', 64 from: 'a', 65 to: 'b', 66 }, 67 { 68 from: 'c', 69 to: 'd', 70 }, 71 { 72 from: 'e', 73 to: 'f', 74 } 75 ] 76} 77
Version information
Replace in files requires Node 12 or higher. (v.3.0.0 +) potentially still supported earlier node, but in pipeline eslint required node >=12 Replace in files requires Node 8 or higher. (v.2.0.3) - potentially still supported node 6, but in pipeline eslint required node 8 Replace in files requires Node 6 or higher. (v.1.1.4)
License
(MIT License)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 1/8 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 29 are checked with a SAST tool
Reason
27 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-44c6-4v22-4mhx
- Warn: Project is vulnerable to: GHSA-4x5v-gmq8-25ch
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
1.9
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to replace-in-files
@rollup/plugin-replace
Replace strings in files while bundling
replace-in-file
A simple utility to quickly replace text in one or more files.
replace-in-files-cli
Replace matching strings and regexes in files
file-replace-loader
file-replace-loader is webpack loader that allows you replace files in compile time