Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

317
Vistas
Node.js fs ENOENT error

I am currently working on an ExpressJS app that reads a yaml file to json, and then sends the json to the front end to be used by Angular.

Here is the file structure for the project

server/
├── data/
│   └── file.yml
├── index.js
└── routes/
    └── index.js

In ./routes/ I have my index.js file which, surprise surprise, contains my routes. And here is the route that I am using to access the yml file:

app.get('/api/assets', (req,res) => {

    try {
      let doc = yaml.safeLoad(fs.readFileSync('../data/file.yml', 'utf8'));
      // console.log(doc);
      res.json(doc);
    } catch (err) {
      let doc = {
        "name": err.name,
        "reason": err.reason,
        "message": err.message
      }
      res.json(doc);
    }

  });

For whatever reason, after spinning up the server and hitting the /api/assets route, I get the following error:

{
  "name": "Error",
  "message": "ENOENT: no such file or directory, open 'tenant-assets.yml'"
}

I have tried a few things to get it working, such as changing the file name to ./data/file.yml and data/file.yml, thinking that it was calling them from the entry index.js. So, I am sort of stuck and don't know what is going on.

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

To use a relative path from the current file, the recommend way is to use path.join() and __dirname.

In your case:

const path = require('path');

app.get('/api/assets', (req, res) => {
    try {
        let doc = yaml.safeLoad(fs.readFileSync(path.join(__dirname, '../data/file.yml'), 'utf8'));
        // console.log(doc);
        res.json(doc);
    } catch (err) {
        let doc = {
            "name": err.name,
            "reason": err.reason,
            "message": err.message
        }
        res.json(doc);
    }
});
about 4 years ago · Santiago Trujillo Denunciar

0

I realized that I was spinning up the server from what is effectively the root directory of the app. In order to fix it, I had to change the path to the file to ./server/data/file.yml.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda