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

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

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