If I understand the question correctly, you want to always write to a path relative to the current script. To get the name of the directory that the currently executing script resides in, you can use __dirname
like so:
var path = require('path');
helper.write = function(data,filename){
if(typeof data !== "string") data = JSON.stringify(data);
var file = path.join(__dirname, 'output', filename);
fs.writeFileSync(file, data);
};
That being said, I don't think it's good practice to be writing files inside of your node_modules
directory. I'd recommend that your module require the full path to a file somewhere else in the file system. If the caller wishes to write to an output directory in its own project tree, you can again use the same __dirname
trick.