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

195
Visualizações
How to get process.argv inputs and write them to a file using fs.writeFileSync

I'm learning node.js and I want to get the inputs from the user and write them to a file called points.txt

const process = require("process")
const fs = require("fs")
const [, , num1,num2,num3,num4] = process.argv
fs.writeFileSync('points.txt', process.argv[2,3,4,5])
node app.js 1 2 3 4

However, with this code, I only see 4 If I were to then go into the points.txt. I should see 1 2 3 4

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

0

The expression process.argv[2,3,4,5] redounds to process.argv[5] (those comma operators just evaluate each int and the whole expression's value ends up as the value of the last int).

Since the code has assigned the params it wants to write as the num variables, write those...

const process = require("process")
const fs = require("fs")
const [, , num1,num2,num3,num4] = process.argv
console.log(num1, num2, num3, num4)
fs.writeFileSync('points.txt', [num1,num2,num3,num4])
about 4 years ago · Juan Pablo Isaza Relatório

0

You can't use the , operator for getting several values that way. The comma operator returns it's last operand, so your code is equal to process.argv[5] which should return 4 in this case. You can use the slice method instead:

process.argv.slice(2);

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

For converting the returned array of the slice call into a string, you can use the join method: process.argv.slice(2).join(' ')

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