Node.js 文件系统访问 returns ENOENT 尽管文件存在

Node.js FileSystem access returns ENOENT despite the file existing

我有一个 node.js 函数,它使用 fs.access 检查文件是否存在,是否可读和可写:

function WhosebugFunction() {
try {
    fs.accessSync(`file://${__dirname}/config/config.ini`, fs.constants.F_OK | fs.constants.R_OK | fs.constants.W_OK);
} catch (err) {
    console.log(err);
}

尽管文件存在,

{ Error: ENOENT: no such file or directory, access 'file:///home/callcenter1/BookGenerator/config/config.ini'
at Error (native)
at Object.fs.accessSync (fs.js:248:11)
at Object.fs.accessSync (ELECTRON_ASAR.js:420:27)
at getConfig (/home/callcenter1/BookGenerator/main.js:12:12)
at Object.<anonymous> (/home/callcenter1/BookGenerator/main.js:18:10)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
errno: -2,
  code: 'ENOENT',
  syscall: 'access',
  path: 'file:///home/callcenter1/BookGenerator/config/config.ini' }

我从 Ubuntu 终端使用了 "Open Link" 函数,它成功地用 gedit 打开了文件。

有什么问题?

编辑 1:

我几乎可以肯定路径是正确的:

使用正确的文件系统路径,没有方案:

fs.accessSync(`${__dirname}/config/config.ini`, ...)

由于完全不同的原因,我遇到了类似的错误消息。如果您根据某些环境变量设置路径,则可能会发生这种情况,因为额外的 (space) 添加到 Windows 上的变量值。然后该消息将看起来很模糊,就像您确定不存在的现有文件一样:

{ "errorMessage":"The file at x:\path\to\file.ext  does not exist, or it is not a file. ENOENT: no such file or directory, lstat 'x:\path\to\file.ext '" }

注意 file.extdoes 之间的 (双 space)。

我使用了类似

的语法
SET "VARIABLE_NAME=VALUE-WHERE-EXTRA-SPACE-CAUSES-ERROR";

解决问题。在这里找到:https://superuser.com/a/1290292