
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
read-modify-write
Advanced tools
NodeJS - Read File, Modify Content, Write to File

This Node module provides an API to interact with the file system and perform recursive sequential IO operations (such as file reading/writing and file content modification). It executes all operations in a blocking and synchronous way. It is a very simple yet very flexible toolkit with size of less than 2KB and no dependencies. To use the library all we need is to import read-modify-write module into our code.
const rmw = require('read-modify-write');
To read file content recursively in a chosen directory we simply need to pass the folder path as the first parameter to the already imported module:
rmw('src/dummy');
To copy files from one directory to another we need a path modifier function which is passed as a second parameter:
const move = filePath => filePath.replace('src', 'dist');
rmw('src/dummy', move);
The path modifier function takes one parameter which is the location / path for each of the listed files. The code above copies all files in the 'src/dummy' folder to the 'dist/dummy'.
To filter files with certain extension we need to supply a function that will be called on each files on the list, and only return the values that pass a specific test (in this case the file extension is present in the string):
const move = filePath => filePath.replace('src', 'dist');
const filterHtml = c => c.endsWith('.html');
rmw('src/dummy', move, filterHtml);
This function, in combination with the previous one can be used to add suffix for certain file types:
const move = filePath => filePath.replace('.js', '.min.js');
const filterJs = c => c.endsWith('.js');
rmw('src/dummy', move, filterJs);
Consider an html file in 'src' folder with the following content:
<html lang="en"><head><meta charset="utf-8"><title></title></head></html>
To update the file content and save it to a new location we need to pass a forth parameter which is an update function which accepts file content as an argument.
const move = filePath => filePath.replace('src', 'dist');
const filterHtml = c => c.endsWith('.html');
const update = c => c.replace('<title></title>', '<title>Page title</title>');
rmw('src/dummy', move, filterHtml, update);
The code above will result in a new html file created in the 'dist/dummy' folder with the following content:
<html lang="en"><head><meta charset="utf-8"><title></title></head></html>
This toolkit can assist you automate painful or time-consuming tasks in your development workflow. For example when publishing to production it can help you by doing CSS preprocessing, JS transpiling, minification, and much more:
const terser = require('terser');
const cleanCss = require('clean-css');
const uglify = c => terser.minify(c).then(({ code }) => code);
const clean = c => new cleanCss().minify(c).styles;
const move = filePath => filePath.replace('src', 'dist');
const filterJs = c => c.endsWith('.js');
const filterCss = c => c.endsWith('.css');
rmw('src', move);s
rmw('src/dummy', move, filterJs, uglify);
rmw('src/dummy', move, filterCss, clean);
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can run the tests the following command:
$ npm test
FAQs
Node module for reading, modifying and writing files recursively
The npm package read-modify-write receives a total of 7 weekly downloads. As such, read-modify-write popularity was classified as not popular.
We found that read-modify-write demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.