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

133
Visualizações
First option should be filled for second option in Node.js

Codes of global.js file of my global package:

let fileName = '',
    port = '';


newArgv = process.argv.slice(2);

if (newArgv[0] === '-db' && newArgv[1] !== '') {
    fileName = newArgv[1];
} if (newArgv[2] === '-port' && newArgv[3] !== '') {
    port = newArgv[3];
}

jsonSpot(fileName, port);

And jsonSpot(fileName, port); is in another file which is the code below:

myFunction = (entryFileName, entryPort) => {

    let fileName = entryFileName || 'db.json',
        port = entryPort || 4000;

...

I open my CMD and write the command below:

myPackage --db myDb.json --port 3000

And it works.

The command below, works too (port 4000 is considered by default as I've written):

myPackage --db myDb.json

But the another command doesn't and always port 4000 is considered by the rest of my code (here port 3000 should be considered):

myPackage --port 3000

What's the problem with my code?

Is there anything wrong with my commands codes or they are related to other parts (absolutely other parts aren't being shown)?

Thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In that case you have only 1 param which is "port", but your code doesn't handle the case where newArgv[0] === '-port'.

Consider using a package like "commander" to handle command line args.

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