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

193
Vistas
Get the caller's directory root from a node.js package

Imagine a directory structure like this:

module
  index.js

project
  main.js
  file.yaml

Imagine further that inside the module/index.js file, there is this code:

const fs = require('fs');

exports.test = function() {
  // How should I *dynamically* reference the caller's app root directory?
  console.log(fs.readFileSync('file.yaml', 'utf8'));
}

Inside the project/main.js file, there is this code:

const x = require('./module');

x.test();

And inside the project/file.yaml file, there is this code:

foo: bar

I want x.test(); to output the contents of project/file.yaml, somehow dynamically referencing the caller's root directory.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Is this what you wanted? __dirname gives the root dir of this file. So, no matter where this function gets called it will have correct location of file.yaml.

const fs = require('fs');

exports.test = function(fileLocation) {
  // How should I *dynamically* reference the caller's app root directory?
  console.log(fs.readFileSync(fileLocation), 'utf8'));
}
// Calling location
test(path.join(__dirname, './file.yaml')
about 4 years ago · Juan Pablo Isaza 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