首页 > 解决方案 > 错误:ENOENT:没有这样的文件或目录,即使文件存在也取消链接

问题描述

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

目录存在

代码:

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'
}

标签: node.jsfilefs

解决方案


In your image file name is different, I believe wrong screen shared,

Also whenever dealing with fs and file paths use path module and __dirname

you can update your path as below

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

推荐阅读