I have found many variations of this question, but none seem to answer my question. I read How do I define a nodejs variable using a function? among many other answers. I tried all of the possible answers using an asynchronous call. It is possible that the answer is in there, but I implemented it wrong.
I am able to get the console.dir command to work inside the function. I am having trouble understanding how to get the console.dir function to work outside of the function.
I would like to set the variable "library" using the contents of a file using the nodejs function fs.readFile. This is what I have, but it does not work.
This seems like such a simple problem, so I am certain I just do not understand callbacks or some universal concept. I have been searching Stackoverflow and the web all day.
var library = fs.readFile('./library', function read(err, data) {
if (err) {
throw err;
}
return data
});
console.dir(library);
the return is:
undefined