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

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

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