Replacing a string in Node.js is not easy with beginners if they don’t have much knowledge about it. In this article we will help you replace a string in a file in Node.js with the replace method. Let’s learn about it with the explanation and examples below.
What is a string in JavaScript?
Like many programming languages, String is the most important type in JavaScript. A string is a sequence of characters. Strings in JavaScript are surrounded by either single quotation marks or double quotation marks.
Look at the example below to know more about it.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript String</h2>
<p>This example will help you learn more about String in JavaScript.</p>
<p id="demo"></p>
<script>
// create a string named 'str1' with single quotation marks.
let str1 = 'Hello everyone, this guide will help you learn how to replace a string in a file by Node.js.'
// create a string named 'str1' with single quotation marks.
let str2 =" I am crvt4722."
// create a string named 'str3' by object String.
let str3 = new String(" Date: 11/09/2022.")
document.getElementById("demo").innerHTML = str1+str2+str3;
</script>
</body>
</html>
Ouput

Replace A String In A File Using Node.js
The following line is the content of the data_text.txt file.
Learn To Share IT.
And you want to replace “Learn To Share” With “Know”. Let’s learn about it with the explanation below.
Step 1: Read the file by fs.readFile method
Syntax
fs.readFile( filename, encoding, callback_function )
Parameters
- filename: The name or the path of the file you want to read.
- encoding: The encoding of the file.
- callback_function: It is called after reading the file.
Return Value
It returns the contents/data stored in the file or errors if any.
Look at the example below to know more about the fs.readFile() method.
const fs = require('fs')
fs.readFile('data_text.txt', 'utf-8', (err, data) => {
if (err) return console.error(err)
console.log(data)
});
console.log(‘Done!’)
Output
Learn To Share IT
Done!
Step 2: Use replace method to replace a string in a file
Syntax
str.replace(oldValue,newValue)
Parameters
- str: The contents of the file you read before.
- oldValue: The String you want to change.
- newValue: The String you want to replace with.
Return Value
A new string is replaced.
Look at the example below to learn more about replace() method.
const newString = data.replace(/Learn To Share/gi, 'Know')
console.log(newString)
Output
Know IT
Step 3: Write the content back to the file by writeFile() method
Syntax
fs.writeFile(path, data, encoding, callback)
Parameters
- path: The name or the path of the file.
- data: The contents you want to write.
- encoding: The encoding of the data.
- callback: It is called after writing the file.
Look at the example below to know more about the writeFile() method.
fs.writeFile('data_text.txt', newString, 'utf-8', err2 => {
if (err2) {
console.log(err2)
}
})
Summary
There are three steps to replace a string in a file using Node.js. Follow the guide above to solve this problem. We hope this tutorial is helpful to you. Thanks!
Maybe you are interested:

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R