Check if a File contains a String in Node.js

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Check if a File contains a String asynchronously in Node.js

To check if a file contains a string in Node.js:

  1. Use the fsPromises.readFile() method to read the file.
  2. Use the includes() method to check if the string is contained in the file.
  3. The includes method will return true if the string is contained in the file.
index.js
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');
The code for this article is available on GitHub
The code snippet assumes that there is an example.txt file located in the same directory. Make sure to also open your terminal in that same directory.
example.txt
hello bobbyhadz.com

check if file contains string asynchronously in node js

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.

If you don't provide a value for the 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.

index.js
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');

performing case insensitive check

The code for this article is available on GitHub

Converting the contents of the file and the string to the same case allows for case-insensitive string comparison.

# Check if a File contains a String synchronously in Node.js

To check if a file contains a string in Node.js:

  1. Use the fs.readFileSync() method to read the file.
  2. Use the includes() method to check if the string is contained in the file.
  3. The includes method will return true if the string is contained in the file.
index.js
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'));

check if file contains string synchronously

The code for this article is available on GitHub

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.

index.js
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'));
The code for this article is available on GitHub

Converting the contents of the file and the string to the same case allows for case-insensitive string comparison.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev