TypeError: fs.readFile is not a function
11,262
I solved this using jQuery:
var importFile = function() {
console.log("started import");
var file = "/path/to/file.txt";
console.log(file);
$.get(file, function(data) {
var lines = data.split("\n");
var id = 0;
$.each(lines, function(n, elem) {
parseLine(elem, id);
id++;
});
console.log("done parsing.");
}, "text");
console.log("done getting");
};
though it never prints "done getting". I don't know why.
Author by
user2651269
Updated on August 06, 2022Comments
-
user2651269 over 1 year
I'm trying to import a file using javascript, and every time I run, it flashes the same error. I already tried redownloading the file system, and I just downloaded requirejs.
var importFile = function() { console.log("started import"); var fs = require(['fs']); console.log("required!"); fs.exists('articles.txt', function(exists) { if(exists) console.log("found file"); }); fs.readFile('articles.txt', function(err, data) { if(err) { throw err; console.log("error thrown"); } var rawFileData = data.toString().split("\n"); for(i in rawFileData) { console.log(articles[i]); } }); for(var i = 0; i< rawFileData.length; i+=4) { var title = rawDataFiles[i]; var tags = rawDataFiles[i+1].split(","); var content = rawDataFiles[i+2]; var date = rawDataFiles[i+3]; articles.append(new Article(title, tags, content, date)); } }
-
user2651269 almost 10 yearsI think it is actually require not requirejs, sorry. I tried taking the brakets out and it said, "Module name "fs" has not been loaded yet for context: _. Use require([])".
-
user2651269 almost 10 yearsdid some more digging and its not require. I got the API from this site: requirejs.org sorry for the confusion. my best guess is that I don't have fs on my computer, so when it tries to do anything there is nothing there. I have tried downloading node.js, but I don't know how to use it.
-
Daniel A. White almost 10 years@user2651269 so this is running in a browser? browsers don't have access to the file system this way.
-
user2651269 almost 10 yearsHow can I access the file system in a browser? I am trying to create a web page that can search through a database read into from a file.
-
Daniel A. White almost 10 yearsthat isn't a trivial matter. it has way too many possibilities.
-
Nufosmatic over 4 yearsI think you'll find that your "done getting" comes out BEFORE the contents of the file. There is something asynchronous about $.get() (that I am not [yet] clever enough to understand).