0

I'm new to node.js and the tutorial I am following always calls functions in the end using JSONconvert(process.argv[2],process.argv[3]), or just sometimes with JSONconvert(process.argv[2]). My question is what is the use of process.arv[2] because even if I run the code without it, it still works. If I do a console.log it prints undefined. For reference here is the .js code I am working on which converts a CSV to JSON.

const fs= require('fs');
const path=require('path');
const csv=require("csvtojson");
const csvFilePath = path.join(__dirname, 'customer-data.csv')
const jsonFilePath = path.join(__dirname, 'customer-data.json') 

const JSONconvert = () => {
    console.log('Converting from CSV file to JSON file ...')

    const converter = csv().fromFile(csvFilePath)

    converter.then((jsonObj) => {
        fs.writeFileSync(jsonFilePath, JSON.stringify(jsonObj, null, 2))
    })

    converter.on('error', (error) => {
        console.log(`error: ${error}`)
        process.exit(1)
    })

    converter.on('done', () => {
        console.log('Successfully converted at', jsonFilePath)
    })
}

JSONconvert(process.argv[2],process.argv[3]);
oniramarf
  • 843
  • 1
  • 11
  • 27
Moon
  • 72
  • 1
  • 9
  • 1
    Possible duplicate of [Could someone explain what "process.argv" means in node.js please?](https://stackoverflow.com/questions/22213980/could-someone-explain-what-process-argv-means-in-node-js-please) – oniramarf Feb 25 '19 at 14:10

1 Answers1

0

The full command line for calling a node script is node script arg1 arg2 .... These elements are held in the array process.argv. Element 0 is always node, 1 is always your script name, everything afterwards are arguments supplied on the command line. If you supply none they will be undefined.

For more information:

Euan Smith
  • 2,102
  • 1
  • 16
  • 26