2

I am completely new to working with JavaScript libraries, but I can't seem to figure out what I am doing wrong here. I am building a browser extension using JavaScript, and I need to be able to parse through a .csv file that I am using as a database. I am trying to work with PapaParse, a JS library for parsing through CSV files, although I can't figure out how to get it into a .js file (my content script, in this case). I'm working in NetBeans. I've added the PapaParse folder to the extension's folder, and I'm trying this to get it into my js file:

var pp = document.createElement("script"); 
pp.src = "PapaParse/papaparse.js"; 
pp.onload = function(e){ 
    console.log("papa parse works here!");
};  

and then I am calling it using:

Papa.parse("data.csv", {
    header: true,
    complete: function(results) {
            console.log("Finished:", results.data);
    }
});

But I am getting an error here saying "Uncaught Reference Error: Papa is not defined". I know there is something wrong with how I am importing the library, but I can't figure out what it is. I'm sure it's something simple that I am missing, so any guidance would be greatly appreciated!

MRB
  • 221
  • 2
  • 12

1 Answers1

1

Try to import it with the import keyword

import Papa from './PapaParse/papaparse';

If there are multiple methods you can import the entire library

import * as myModule from './PapaParse/papaparse';

And from this the calling should work if it's in the same file as the import.

Blaj Bogdan
  • 415
  • 1
  • 3
  • 17
  • Hi! I've tried both suggestions and either way I keep getting this error-- Uncaught SyntaxError: Cannot use import statement outside a module. Do you know why this is? – MRB Aug 20 '20 at 19:26
  • Can you provide more detail of your code? If you are working inside an html file you will need this , the module tag should do it, provide more details regarding the code if this doesn't do the job. – Blaj Bogdan Aug 23 '20 at 21:28
  • I said in the description of my question that I am working inside a JavaScript file. I'm not sure what other parts of my code are relevant to this, what would you like to see? The only other thing I'm not sure of is if I need to include the PapaParse file somewhere in my manifest.json. – MRB Aug 25 '20 at 19:05