'NodeJS: How to get a path a module was imported from

I have a module with some exported function that creates a file.

fs.appendFile(`${__dirname}/file_name`, 'aaaa', () => {});

When I import this module using a require syntax, when calling the function, the file is created in .node_modules/my_module/ (probably as expected). The question is how can I make it to create a file in the folder I'm calling this function.

I don't have an option to inject the path when calling the function, in the real app it's called by a third party reflection mechanism.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source