1

When I run my code:

var fs = require('fs');
var text = fs.readFileSync('data.txt').toString().split("\n");

function getDataJS(){
    var i = 0;
    var c = "";
    var d = "";
    var t = "";
    var r = "";
    for(i = 0;i<text.length;i++){
        if(i == 0){
            c = text[i];
            document.getElementById('con').innerHTML = c;
        } else if(i == 1){
            d = text[i]
            document.getElementById('dec').innerHTML = d;
        } else if(i == 2){
            t = text[i]
            document.getElementById('tes').innerHTML = t;
        } else if(i == 3){
            r = text[i]
            document.getElementById('rec').innerHTML = r;
        }
    }
}


module.exports = getDataJS();

(with my html) I get:

Uncaught TypeError: fs.readFileSync is not a function

Does anyone know why this is? I know that node does not work with browsers, but I'm confused why browserify isn't working.

  • If you're running this code in a browser, you can't open files from a browser context – ADyson Apr 18 '20 at 19:26
  • Hey Daniel, the browser doesn't have all the methods that node.js does. And vice versa. Try using the [File API](https://stackoverflow.com/questions/750032/reading-file-contents-on-the-client-side-in-javascript-in-various-browsers) – Josh Bradley Apr 18 '20 at 19:42

1 Answers1

2

From Browserify:

Browsers don't have the require method defined, but Node.js does. With Browserify you can write code that uses require in the same way that you would use it in Node.


Browserify let's you use require. It doesn't let you use the APIs provided by Node.js that are not provided by browsers… including those needed to read files from the computer the code is running on.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Is there an alternative way to do what I'm trying to do? – Daniel Outis Apr 18 '20 at 19:39
  • @DanielOutis — I don't know what you are trying to do. https://en.wikipedia.org/wiki/XY_problem – Quentin Apr 18 '20 at 19:40
  • I'm trying to feed live data to my website that is updated every 10 minutes from a file 'data.txt'. I have a python file which scrapes the data from a website, then replaces the text in data.txt with the new data, then I need the javascript file to take that data and display it on the html site. – Daniel Outis Apr 18 '20 at 19:53
  • @DanielOutis does the file get placed on the server? in that case your browser can use AJAX to fetch the contents from the server. – ADyson Apr 18 '20 at 19:57
  • If you're trying to make a HTTP GET request from JavaScript then you should probably ask a question about that (or do a search, its a common problem). – Quentin Apr 18 '20 at 19:58
  • the file does get placed on the sftp server. @ADyson – Daniel Outis Apr 18 '20 at 20:00
  • @Quentin does the http get code go in the html or js? – Daniel Outis Apr 18 '20 at 20:04
  • @DanielOutis you can't make an FTP or SFTP connection from browser-based code. You need to be able to access the file via HTTP or HTTPS – ADyson Apr 18 '20 at 20:06
  • @DanielOutis HTML is a visual markup language, it can't be used for programming, so yes the HTTP code would need to be written in the Javascript. I suggest you find a tutorial showing you how to make an AJAX request using the `fetch()` functionality built into all modern browsers. – ADyson Apr 18 '20 at 20:06