Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

508
Visualizações
fs.readFileSync says 'no such file or directory' although require() returns the file correctly

I need to read a JSON file dynamically. This is why require() won't cut it. It doesn't simply update the file until NodeJS server restarts. This is why I have to use fs.readFile or fs.readFileSync.

When I use, const data = require("../data.json"); it just reads it properly. No issues at all, but like I said, it doesn't update dynamically.

When I use const data = () => fs.readFileSync("../data.json", { encoding: "utf8" }); it simply returns Error: ENOENT: no such file or directory, open '../data.json'

I also tried fs.readFile. Doesn't work.

Most weirdly, when I try to read another json file with fs.readFileSync, I'm seeing values that I changed days ago. It reads an old version. I don't understand. That old version doesn't exist anymore. I manually checked.

I tried npm install, npm cache clean --force. I can't think of anything else.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

require takes a path relative to the directory the module file is in.

readFileSync takes a path relative to the current working directory.

These must be different for you.

Consider using path.resolve with your relative path and __dirname

about 4 years ago · Juan Pablo Isaza Relatório

0

You're probably looking for require.resolve if you're working with CommonJS modules in Node. This will use the internals of require to provide the absolute path string to the file you want to read in.

Source: https://nodejs.org/api/modules.html#requireresolverequest-options

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda