Error: ENOENT: no such file or directory, unlink even tho the file exists

Error: ENOENT: no such file or directory, unlink even tho the file exists

如您所见,目录存在,文件也存在,但由于某种原因,无法找到要删除的文件。

Directory exists

代码:

const fs = require('fs')

const path = "./resources/castigos/silencios/705143456384811039/526122991042428959.json"

fs.unlink(path, (err) => {
  if (err) {
    console.error(err)
    return
  }

  //file removed
})

我尝试 运行 项目作为 root,没有...

[Error: ENOENT: no such file or directory, unlink './resources/castigos/silencios/705143456384811039/526122991042428959.json'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'unlink',
  path: './resources/castigos/silencios/705143456384811039/526122991042428959.json'
}

你的图片文件名不一样,我相信共享的屏幕有误,

此外,每当处理 fs 和文件路径时使用 path 模块和 __dirname

您可以如下更新您的路径

const path = require('path');
const filePath = path.join(__dirname, '/resources/castigos/silencios/705143456384811039/526122991042428959.json');