Use the built-in fs module in NodeJS to read the contents of the text file. This can be done using the fs.readFileSync() method, which returns the contents of the file as a string.

Split the contents of the file into an array of lines using the split() method. This method splits a string into an array of substrings based on a specified separator.

Create an empty object to store the key-value pairs. Loop through the array of lines, and for each line, split it into key-value pairs using the split() method again. This time, use a separator that separates the key and value, such as a colon or equal sign. Add the key-value pairs to the object created in step 3.

You can see the sample code snippet that shows how to implement these steps:

const fs = require('fs');

// Read the contents of the file
const contents = fs.readFileSync('myfile.txt', 'utf8');

// Split the contents into an array of lines
const lines = contents.split('\n');

// Create an empty object to store the key-value pairs
const data = {};

// Loop through the lines and split them into key-value pairs
lines.forEach(line => {
  const [key, value] = line.split(':'); // Use the appropriate separator
  data[key.trim()] = value.trim(); // Add the key-value pair to the object

console.log(data); // Print the object to the console

This code assumes that the text file has key-value pairs separated by colons. You can adjust the separator used in the split() method depending on the format of your text file.