最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ENOENT: no such file or directory, open 'Filename' - Stack Overflow

programmeradmin1浏览0评论

I was requiring some data from a .json file but I am getting this error:

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

My code:

let shop_data = JSON.parse(Buffer.from(fs.readFileSync('../Jsons/eshop.json')).toString());
    let index = (args[0] || "1");
    let page = shop_data.pages[index];

I think that's all you need, but if any other code was needed, ment it. I am using discord.js v13 and node.js 16

I was requiring some data from a .json file but I am getting this error:

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

My code:

let shop_data = JSON.parse(Buffer.from(fs.readFileSync('../Jsons/eshop.json')).toString());
    let index = (args[0] || "1");
    let page = shop_data.pages[index];

I think that's all you need, but if any other code was needed, ment it. I am using discord.js v13 and node.js 16

Share Improve this question asked Apr 7, 2022 at 6:57 PooyanPooyan 4901 gold badge9 silver badges22 bronze badges 2
  • readFileSync already return buffer, no need Buffer.from function. FYI – deko_39 Commented Apr 7, 2022 at 9:38
  • Don't use absolute paths, it won't work. – Goodies Commented Jan 4, 2023 at 15:46
Add a ment  | 

3 Answers 3

Reset to default 2

You need to use path module and join the folder names to define the json file location.

Let,the data is in following directory. ( NewProject/data/eshop.json )

  • NewProject
    • Project root directory
    • data
      • eshop.json

You want to access the eshop.json file from any file under the project directory.

Solution is below:

const fs = require('fs')
const path = require('path');
const directory = path.join('data', 'eshop.json')
// directory ==> data\eshop.json
exports.getRandomUserService = () => {
     const jsonData = fs.readFileSync(directory);
     const eshop= JSON.parse(jsonData);
     console.log('eshop data from json file: ',eshop);
}

The issue is with the path. It seems the path is invalid for the given eshop.json file or there might be any spelling mistake in the path.

fs.readFileSync takes the relative path:

  • test.js
  • JSON
    • sampleJSON
      • eshop.json
fs.readFileSync('./JSON/sampleJSON/eshop.json');

I suggest the problem came from module reading path, if your case is fs.readFile is working find until you call it from root index.js

If Your Path
-index.js
./services/getData.js (fs.readFile func is calling here)
./json/eshop.json

Inside index.js

app.get( '/eshop', getData);

Have to change view side of ./services/getData.js to index.js

'../json/eshop.json'

To

'./json/eshop.json'

I got the same issue, this fix me, no need to install anything.

发布评论

评论列表(0)

  1. 暂无评论