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

723
Views
Get the path of the project in NodeJS

I'm developing a package for logging in Node.js. I want to able to read the path of the project where my package is being used. That's because I want the user to be able to put a config file in the root of their project. I want to achieve something similar as the ESLint package being able to read the .eslintrc file.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I want to able to read the path of the project where my package is being used

process.argv[1] will be the fully qualified path of the root script that was executed (perhaps without file extension).

So, you can take that fully qualified path and then get just the directory like this:

const rootdir = path.dirname(process.argv[1]);

As you asked, this is the directory where the root script that started the whole nodejs program is located. That may or may not be where your particular module was loaded from. Your module could be loaded from some other module which was itself loaded from the root script.

Also, this is not necessarily the same directory as the current working directory, process.cwd(). If a path is passed to the root script, then the current working directory can literally be anything so it cannot be counted on to point at any specific module.


Note that good modularity will NOT have dependencies on specific parent modules or specific parent modules being located in certain places. If your module needs to know where some resources are located, it should be able to find them in one of several ways:

  1. In it's own directory (accessible via __dirname or import.meta).
  2. In some configuration data passed into the module at initialization time.
  3. In a location specified in either the environment or a config file in a known location.

You should generally not write a module that must have certain parent modules in known locations. Somewhat by definition, that's not an easily shareable, reusable module if it can only be used by a specific other module that has a specific structure.

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!