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

230
Views
Get installed package root path from within package

I am building an npm package which is basically an CLI tool. It is intended to be installed globally and used as CLI with dist/clj.js, but can also be installed as a regular dependency and accessed via dist/index.js.

From inside .js files of my package (like dist/cli.js or dist/index.js) I want to get an absolute path to the root of my package (where package.json is located).

I know this can be done with __dirname but for different files in my project it require different actions:

// dist/cli.js
console.log(require('path').join(__dirname, '..'));

// dist/a/b/c/foo.js
console.log(require('path').join(__dirname, '..', '..', '..', '..'));

Is there a better way to get the root of both globally or locally installed package from any .js file inside this package regardless of the folder this package is used in?

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

0

This code seems to work from any file within package:

const path = require('path');

let packageRootPath = path.join(path.dirname(require.main.filename), '..');

.. in the end needed to jump from src/dist to the root since all my js files are located in src/dist forders.

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!