I have a executable node / javascript script that has a debug boolean, if set to true a couple of files will be written. This executable is also node module. Depending on the working directory of the user running the script it seems that the function can't find the directory to write files into.
The module is structured like this
output/
lib/
helpers.js
index.js
My original reasoning would be to have the path be.
helper.write = function(data,filename){
if(typeof data !== "string") data = JSON.stringify(data);
fs.writeFileSync("./output/"+filename, data);
};
However this works when running the script from within the node_module folder
fs.writeFileSync("process.cwd()+"/node_modules/the_module/output/"+filename, data);
Like this
node ./my_app/node_modules/the_module/index.js
This gets even more confusing if the modules is used in another executable file that uses the library.
node ./my_app/run.js
Is there a way to save the file independent from all of these variables?