0

I try to use csvtojson

const csv = require("csvtojson");
const csvFilePath = require('./../../books.csv');
csv()
.fromFile(csvFilePath)
.then((jsonObj)=>{
    console.log(jsonObj);
})

My file.csv is

Book,Author,Amount,Price
The Compound Effect,Darren Hardy,5,9.48
The 7 Habits of Highly Effective People,Stephen R. Covey,4,23.48
The Miracle Morning,Hal Elrod,10,21.34
Influence: The Psychology of Persuasion,Robert B. Cialdini,4,12.99
The ONE Thing,Gary Keller,1,11.18

And I have the next error

The Compound Effect,Darren Hardy,5,9.48
^^^^^^^^

SyntaxError: Unexpected identifier
 at Module._compile (internal/modules/cjs/loader.js:723:23)

What is wrong? Thanks)

Slava
  • 161
  • 1
  • 5

3 Answers3

2

The problem here is that you're using the function require('./../../books.csv'); to import your file.

After reading the documentation I can assure that you only have to pass the path to the CSV file.

So, your code should be like:

const csv = require('csvtojson');
const csvFilePath = './../../books.csv';

csv().fromFile(csvFilePath).then((jsonObj)=>{
    console.log(jsonObj);
})

And the desired output should be:

[ { Book: 'The Compound Effect',
    Author: 'Darren Hardy',
    Amount: '5',
    Price: '9.48' },
  { Book: 'The 7 Habits of Highly Effective People',
    Author: 'Stephen R. Covey',
    Amount: '4',
    Price: '23.48' },
  { Book: 'The Miracle Morning',
    Author: 'Hal Elrod',
    Amount: '10',
    Price: '21.34' },
  { Book: 'Influence: The Psychology of Persuasion',
    Author: 'Robert B. Cialdini',
    Amount: '4',
    Price: '12.99' },
  { Book: 'The ONE Thing',
    Author: 'Gary Keller',
    Amount: '1',
    Price: '11.18' } ]
Ricardo Faria
  • 764
  • 13
  • 30
  • If I do as you say above, I have the next error 'Unhandled rejection Error: File does not exist. Check to make sure the file path to your csv is correct.' – Slava Nov 20 '19 at 07:43
  • The problem you have by using the function 'require' is that this function reads a JavaScript file, executes the file, and then proceeds to return the exports object. It is not supposed to read CSV files. About the file path, you just have to double check it. – Ricardo Faria Nov 20 '19 at 09:29
  • As I don't have access to your machine is difficult to know where the file is located, so I just supposed that './../../books.csv' was the path to your file. – Ricardo Faria Nov 20 '19 at 09:30
  • Thank you, it solved the problem – Lipe Aug 02 '23 at 18:16
2

The problem was resolved by changing the path)

const csvFilePath = './../../books.csv';

to

const csvFilePath = `${__dirname}/../../books.csv`;

However, I do not think it is a great idea to use '__dirname' in this case. If you have any ideas about it, please write it)

Slava
  • 161
  • 1
  • 5
  • __dirname gives the current directory path where this JS file is located, the other part of the path depends on the location of the books.csv file. So, in this case, if the CSV file directory is above the JS file directory, it's just fine. E.g. https://stackoverflow.com/a/7083067/7673764 – Ricardo Faria Nov 20 '19 at 09:42
0

I think you should have " in your .csv file or use quote for processing options

https://www.npmjs.com/package/csvtojson#parameters

hejkerooo
  • 709
  • 2
  • 7
  • 20
  • In my opinion, the issue is not in 'quote'( I try to use the example from documentation https://www.npmjs.com/package/csvtojson#from-csv-file-to-json-array and I have the same problem. – Slava Nov 19 '19 at 23:08