ENOENT:没有这样的文件或目录,打开“文件名”

Poo*_*yan 6 javascript node.js discord discord.js

我需要来自 .json 文件的一些数据,但收到此错误:

Error: ENOENT: no such file or directory, open '../Jsons/eshop.json'
    at Object.openSync (node:fs:585:3)
    at Object.readFileSync (node:fs:453:35)
    at Object.execute (C:\Users\Pooyan\Desktop\PDM Bot Main\commands\shop.js:9:24)
    at module.exports (C:\Users\Pooyan\Desktop\PDM Bot Main\events\guild\message.js:114:15)
  errno: -4058,
  syscall: 'open',
  code: 'ENOENT',
  path: '../Jsons/eshop.json'
}
Run Code Online (Sandbox Code Playgroud)

我的代码:

Error: ENOENT: no such file or directory, open '../Jsons/eshop.json'
    at Object.openSync (node:fs:585:3)
    at Object.readFileSync (node:fs:453:35)
    at Object.execute (C:\Users\Pooyan\Desktop\PDM Bot Main\commands\shop.js:9:24)
    at module.exports (C:\Users\Pooyan\Desktop\PDM Bot Main\events\guild\message.js:114:15)
  errno: -4058,
  syscall: 'open',
  code: 'ENOENT',
  path: '../Jsons/eshop.json'
}
Run Code Online (Sandbox Code Playgroud)

我认为这就是您所需要的,但如果需要任何其他代码,请对其进行评论。我正在使用discord.js v13和node.js 16

Apo*_*ara 1

问题出在路径上。似乎给定eshop.json文件的路径无效,或者路径中可能存在拼写错误。

fs.readFileSync采取相对路径:

  • 测试.js
  • JSON
    • 样本JSON
      • eshop.json
fs.readFileSync('./JSON/sampleJSON/eshop.json');
Run Code Online (Sandbox Code Playgroud)