Node.js TypeError: path must be a string or Buffer
Change line
let stream = fs.createReadStream(program.list)
to
let stream = fs.createReadStream(program.argv[some number])
where some number is position at which you mention filename
e.g. run program with following command
node test.js somevar filename
then somenumber = 3
0th param > node
1st param > test.js (file to run)
2nd > somevar
3rd > filename
Another error:
Final code will look like
const csv = require('fast-csv');
const fs = require('fs');
console.log("hello world")
console.log("list of order prices", process.argv[2]);
let required_products=[]
for(var i=3;i<process.argv.length;){
let temp=[]
temp.name=process.argv[i++]
temp.quantity=process.argv[i++]
required_products.push(temp)
}
/*
To read csv file and print the data to the console:
[node orderPrice --list input/sample.catalog.csv]
*/
let stream = fs.createReadStream(process.argv[2]);
var total = 0;
var csvStream = csv()
.on("data", function(data){
let product_name = data[0];
let quantity = data[1];
let price = data[2];
required_products.forEach(function(product){
if(product['name']==product_name){
if(parseInt(product['quantity'])>parseInt(quantity)){
console.log('Quantity required for product '+product['name']+' '+product['quantity']+' is greater than available '+quantity);
process.exit(1)
}else{
total += parseInt(price) * parseInt(product['quantity']);
}
}
})
})
.on("end", function(){
console.log("done");
let totalWithVat = total * (1+ 0.23);
console.log("Total price:", totalWithVat);
}).on("error", function (error) {
console.error("The following error occured:", error);
})
stream.pipe(csvStream);