There will be times when you need to check if a file contains a string in NodeJS. In this tutorial, we will show you how to do that using the include() method. Keep reading for detailed instructions.
Check if a file contains a string in Node.js
Applying the fs.readFileSync() method
The first approach we recommend to you is using the fs.readFileSync() method. You can follow these steps:
- Use the fs.readFileSync() method to read the file. You can choose to read files synchronously or asynchronously.
- Use the include() method to check if the file contains a string
- If the result is ‘true’ then the string is in the file, and vice versa.
To illustrate, take a look at the following example:
Suppose we create the file ‘demo.txt’ in the same directory as the index.js file. Here is the content of the file ‘demo.txt’:
Welcome to LearnShareIT community!
Here is the program to check if a file contains a string
// This example use the ES6 Imports import { readFileSync, promises as fsPromises } from "fs"; // Synchronously function checkContainStringSync(file, checkedStr) { const contents = readFileSync(file, "utf-8"); const isContain = contents.includes(checkedStr); console.log(isContain); } checkContainStringSync("./demo.txt", "LearnShareIT"); // Asynchronously async function checkContainStringAsync(file, checkedStr) { try { const contents = await fsPromises.readFile(file, "utf-8"); const isContain = contents.includes(checkedStr); console.log(isContain); return isContain; } catch (error) { console.log(error); } } checkContainStringAsync("./demo.txt", "LearnShareIT");
Output:
true
true
As you can see, the return results of two functions, ‘checkContainStringSync’ and ‘checkContainStringAsync’, are both ‘true’ as we expect.
In both functions, the fs.readFileSync() method takes two parameters and returns the file’s contents.
The first parameter is the path to the file and the second parameter is the encoding. If you do not provide a value for the encoding parameter, this method will return a buffer, otherwise a string.
The rest is to use the include() method to check if the string is in the file content and return the result.
Note: With the async function you must use the ‘async/await’ keyword pair and the include() method is case-sensitive.
Applying the fsPromises.readFile() method
In this approach, we guide you to use the fsPromises.readFile() method to check if a file contains a string or not:
- Use the fsPromises.readFile() method to read the file asynchronously.
- Use the include() method to check if the file contains a string
- If the result is ‘true’ then the string is in the file, and vice versa.
// This example use the ES6 Imports import { promises as fsPromises } from "fs"; // Asynchronously async function checkContainStringAsync(file, checkedStr) { try { const contents = await fsPromises.readFile(file, "utf-8"); const isContain = contents.includes(checkedStr); console.log(isContain); return isContain; } catch (error) { console.log(error); } } checkContainStringAsync("./demo.txt", "LearnShareIT");
Basically, we still do the same as the first approach, and the result is no different.
The fsPromises.readFile() method also takes two parameters similar to the fs.readFileSync() method. However, there are a few things you should keep in mind.
The fsPromises.readFile() method defaults to read the entire contents of a file asynchronously.
The function checkContainStringAsync() does not return a boolean value directly, but instead a Promise that resolves with a boolean value.
Conclusion
In conclusion, you can use the fs.readFileSync() method or the fsPromises.readFile() method to read the file content and the include() method to check if a file contains a string in NodeJS. You can choose to use whichever method you want. That’s the end of this tutorial. Hopefully, the methods we provided in this post will be helpful to you.
Maybe you are interested:
- Get absolute path of file from relative path in Node.js
- Replace a String in a File using Node.js
- Npm WARN old lockfile The package-lock.json file was created with an old version of npm

My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.
Name of the university: HUST
Major: IT
Programming Languages: Python, JavaScript, TypeScript, Node.js, React.js