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

198
Visualizações
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 Respostas
Responde à pergunta

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 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