Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!