I'm trying to load the CONFIG
file from the .git
folder of all of my repositories to parse the data manually and that particular file is extensionless. I'm trying to load it with fs
:
const fs = require('fs');
function loadFile(filepath) {
try {
fs.readFile(filepath, function (exception, data) {
if (exception)
console.error(exception);
else
console.log(data.toString());
});
} catch (exception) {
console.error(exception);
}
}
However, when I put breakpoints in the catch
block and the callback for readFile
, none of them are hit.
How can I load an extensionless file in a Node JS application?
The file you want to read does not need to have an extension,
Just give the correct path to Function.
For example, if the .git folder is next to your js file, you can read config like this:
const fs = require('fs');
function loadFile(filepath) {
try {
fs.readFile(filepath, function (exception, data) {
if (exception)
console.error(exception);
else
console.log(data.toString());
});
} catch (exception) {
console.error(exception);
}
}
loadFile('./.git/config')