Last updated: Apr 5, 2024
Reading timeยท4 min

To check if a file contains a string in Node.js:
fsPromises.readFile() method to read the file.includes() method to check if the string is contained in the file.includes method will return true if the string is contained in the
file.import {readFileSync, promises as fsPromises} from 'fs'; // ๐๏ธ if using CommonJS syntax // const {promises: fsPromises} = require('fs'); // โ read file ASYNCHRONOUSLY async function checkIfContainsAsync(filename, str) { try { const contents = await fsPromises.readFile(filename, 'utf-8'); const result = contents.includes(str); console.log(result); return result; } catch (err) { console.log(err); } } // ๐๏ธ true checkIfContainsAsync('./example.txt', 'hello');
example.txt file located in the same directory. Make sure to also open your terminal in that same directory.hello bobbyhadz.com

The directory structure in the example assumes that the index.js file and the
example.txt file are located in the same folder and our terminal is also in
that folder.
The fsPromises.readFile() method asynchronously reads the contents of the provided file.
encoding parameter, the method returns a buffer, otherwise, a string is returned.The method returns a promise that resolves with the contents of the file, so we
have to await it or use the .then() method on it to get the resolved string.
Note that the checkIfContainsAsync function returns a Promise that resolves
with a boolean value.
It doesn't return a boolean value directly like the synchronous method from the next subheading.
We used the String.includes() method to check if the file's contents contain the provided string.
The includes() method performs a case-sensitive search for whether the
provided string is found in another string.
If you need to do a case-insensitive search, convert the file's contents and the passed-in string to lowercase.
import {readFileSync, promises as fsPromises} from 'fs'; // ๐๏ธ if using CommonJS syntax // const {promises: fsPromises} = require('fs'); // โ read file ASYNCHRONOUSLY async function checkIfContainsAsync(filename, str) { try { const contents = await fsPromises.readFile(filename, 'utf-8'); const result = contents.toLowerCase().includes(str.toLowerCase()); console.log(result); return result; } catch (err) { console.log(err); } } checkIfContainsAsync('./example.txt', 'hello');

Converting the contents of the file and the string to the same case allows for case-insensitive string comparison.
To check if a file contains a string in Node.js:
fs.readFileSync() method to read the file.includes() method to check if the string is contained in the file.includes method will return true if the string is contained in the
file.import {readFileSync, promises as fsPromises} from 'fs'; // ๐๏ธ if using CommonJS syntax // const {readFileSync} = require('fs'); // โ read file SYNCHRONOUSLY function checkIfContainsSync(filename, str) { const contents = readFileSync(filename, 'utf-8'); const result = contents.includes(str); return result; } // ๐๏ธ true console.log(checkIfContainsSync('./example.txt', 'hello'));

The function from the first example reads the contents of a file synchronously.
The fs.readFileSync() method takes the path to the file as the first parameter and the encoding as the second.
The method returns the contents of the provided path.
If you omit the encoding parameter, the function will return a buffer,
otherwise, a string is returned.
The includes() method performs a case-sensitive search for whether the
provided string is found in another string.
If you need to do a case-insensitive search, convert the file's contents and the passed-in string to lowercase.
import {readFileSync, promises as fsPromises} from 'fs'; // ๐๏ธ if using CommonJS syntax // const {readFileSync} = require('fs'); // โ read file SYNCHRONOUSLY function checkIfContainsSync(filename, str) { const contents = readFileSync(filename, 'utf-8'); const result = contents.toLowerCase().includes(str.toLowerCase()); return result; } // ๐๏ธ true console.log(checkIfContainsSync('./example.txt', 'hello'));
Converting the contents of the file and the string to the same case allows for case-insensitive string comparison.
You can learn more about the related topics by checking out the following tutorials: