简体   繁体   English

如何在我的npm模块中写入另一个nodejs项目根文件夹

[英]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). 我正在编写一个npm模块,需要在根文件夹或nodejs项目中的特定文件夹中编写/创建一个文件,该文件将使用我的npm模块(基本上是那些将使用我的npm模块的人)。

How can i do this? 我怎样才能做到这一点?

我相信你可以使用process.cwd()来获取当前的工作目录https://nodejs.org/api/process.html#process_process_cwd

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. 此代码将在项目根目录中的logsFile.log中创建或添加。 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. 如果它是通过npm导入的,它也会在使用该模块的项目中写入,而不是在node_modules项目文件夹中。

So that resolves my question. 这样就解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM