N
N
Night_Harpy2021-01-15 18:13:41
Node.js
Night_Harpy, 2021-01-15 18:13:41

Why does readFile throw an error that the file was not found?

I am trying to read json file. If I do it through require, then it reads everything: but if I do it through readFile or readFileSync, then it gives an error ENOENT: no such file or directory. what could be the problem?
const data = require('./json/name.json')
const data = fs.readFileSync('./json/name.json')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@imegaded, 2021-01-15
_

Try using an absolute path.

const path = require('path');
const file = fs.readFileSync(path.resolve(__dirname, './json/name.json'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question