/ QUICK TIPS

Error using jquery-csv: $.csv is undefined

Javascript has nice tool to read and transform csv data, with handy api but with unexpected inconvenience as well.

Recently in one web project I had to convert csv data to javascript objects. Quick DuckDuckGo research has shown me library which suits my needs: jquery-csv. I did everything the same way as in official demo, but exception $.csv is undefined kept blocking me. My IDE was able to go to declaration without any problems, still browser was unable to got it.

Solutions

Wait till document is ready

The only solution which I found on the internet was to wait till document is ready.

$( document ).ready(function() {
    // Use jquery-csv here
});

Unfortunately this was not my case, I already did it.

Check libraries load order

Finally, comparing my code with demo line by line, I found the solution. I had jquery-csv import after usual jQuery. So all I had to do was moving <script src="jquery-csv.js"></script> line after <script src="jquery-3.4.0.min.js"></script>. That’s because this library tries to follow jQuery api, which seems to be convenient idea, but practice shows that it has big disadvantage - jQuery deletes or mutates library namespace.

Summary

That’s it, but coming to this point took me some time and internet was not helpful back then, so I decided to share this solution. It might be helpful for people stuck in the same situation :)

Tometchy speaker

If you want we can arrange remote consultations. Reach me at tometchy@gmail.com

tometchy

Tometchy

Passionate focused on agile software development and decentralized systems

Read More