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

232
Vistas
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 Respuestas
Responde la pregunta

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