sendra sendra - 2 months ago 6
Javascript Question

How to write in another nodejs project root folder from my npm module

I am writing a npm module that needs to write/create a file in the root folder or in a specific folder in the nodejs project that is going to use my npm module (basically the people who will use my npm module).

How can i do this?

Answer

In the end I found that just writeing the name of the file will put it in the root of the project what ever it is.

function writeLog (logMessage) {
    //  put the date on the message log
    fs.appendFile('logsFile.log', logMessage + '\r\n', (err) => {
        if (err) {
            throw err;
        }
    });
}

this code will create or apend in the logsFile.log in the root directory of the project. If it is imported via npm it will write also in the project that uses that module and not in the node_modules project folder.

So that resolves my question.