ENOENT:在Object.fs.mkdirSync上没有这样的文件或目录

时间:2019-04-04 17:13:22

标签: node.js express

我当前正在启动NodeJS应用程序,我想检查文件夹是否存在操作:

[Node] log/20190405 ----------created the folder log/20190405/2019040500.log-----------------
[Node] fs.js:885
[Node]   return binding.mkdir(pathModule._makeLong(path),
[Node]                  ^
[Node] 
[Node] Error: ENOENT: no such file or directory, mkdir 'log/20190405'
[Node]     at Object.fs.mkdirSync (fs.js:885:18)
[Node]     at fs_1.default.access (/home/lordrauf/Documents/myproject/dist/util/logger.js:25:30)
[Node]     at FSReqWrap.oncomplete (fs.js:135:15)

奇怪的是,我不确定该文件夹位于何处,但是对以下代码段的检查失败:

function checkFilePromise() {
    return new Promise((resolve, reject) => {
        fs.access(`log/${format("yyyyMMdd", new Date())}`, fs.constants.F_OK, (err) => {
            console.log(`log/${format("yyyyMMdd", new Date())} ${err ? `----------created the folder ${logName}-----------------` : '----------log folder exists----------'}`);
            if (err) {
                fs.mkdirSync(`log/${format("yyyyMMdd", new Date())}`);
                console.log(err)
                reject(err)
            } else {
                resolve(true)
            }
        });
    })
}

如果有人知道如何解决此问题,请帮助我。任何建议表示赞赏。谢谢。

0 个答案:

没有答案