-1

In a nutshell, what I want is for a javascript function to check a file for an HTML input. I have tried using doing as this post told me to: Javascript check if (txt) file contains string/variable
But kept getting an error saying ReferenceError: fs is not defined
So now I am here asking for help.

Marc
  • 15
  • 6
  • fs for node not for browser. – Sajeeb Ahamed Apr 03 '20 at 17:30
  • Is this helpful? https://stackoverflow.com/questions/45405517/module-fs-cannot-be-found – Anis R. Apr 03 '20 at 17:30
  • 3
    Hi Kaiden, your question could benefit from some example source code. For future reference please review https://stackoverflow.com/help/how-to-ask – Jeff Mergler Apr 03 '20 at 17:32
  • You might just need to import fs. At the top of your script add: `const fs = require('fs');` – ray Apr 03 '20 at 17:36
  • So understanding that I cannot use fs, but what can I do then? – Marc Apr 03 '20 at 17:37
  • If you're trying to do this in a browser, you could [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) the file and use [includes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) to check for what you're looking for. – ray Apr 03 '20 at 17:53

2 Answers2

1

I cannot be sure because you didn't provide any example code, but fs usually refers to "file system" in nodejs So the answer from the other topic isn't complete but the import is implied by it's usage:

var fs = require("fs")

fs.readFile(FILE_LOCATION, function (err, data) {
  if (err) throw err;
  if(data.indexOf('search string') >= 0){
   console.log(data)
  }
});

But like some other commentors have said, this is for nodejs and not in the browser.

Jason Lydon
  • 7,074
  • 1
  • 35
  • 43
  • [data.includes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) answers the question more directly than `indexOf` and might be more performant. – ray Apr 03 '20 at 17:34
  • @rayhatfield searching for a string in a file (txt file as example) not a string in a string. – Marc Apr 03 '20 at 17:38
  • @KaidenSmith I'm referring to his use of `indexOf` in the callback, where `data` *is a string or buffer*, both of which support `includes`. – ray Apr 03 '20 at 17:45
0

If you're trying to do this in a browser, you could fetch the url and use includes to check for the string you're looking for:

async function responseContains(url, string) {
  try {
    const content = await fetch(url).then(response => response.text());
    return content.includes(string);
  }
  catch (e) {
    console.log(e);
    throw e;
  }
}

const url = 'https://jsonplaceholder.typicode.com/todos/1';
/*
  the url above returns:
  {
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  }
*/

responseContains(url, 'userId')
  .then(found => console.log(`Found 'userId'? ${found}`));
// Found? true

responseContains(url, 'wookies with bananas')
  .then(found => console.log(`Found 'wookies with bananas'? ${found}`));
// Found? false
ray
  • 26,557
  • 5
  • 28
  • 27