Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!