1

I am building a chrome browser extension, and I am trying to use the library PapaParse to parse through a csv file. I am working with a local copy of the library, which I downloaded from their website. I am trying to import the library into a JavaScript file (I am not working with it in an html file), but it is giving me a lot of trouble. I am new to working with libraries, but I feel like it shouldn't be this complicated. I am trying to import it into my content script, but I can also create a separate .js file to work with it if need be. I have tried many different ways to solve this problem, and it has been recommended to me to import it as a module using the import statement. When I try

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

I get the error "Uncaught SyntaxError: Cannot use import statement outside a module". Now, normally it looks like this error is usually solved by adding type="module" to the script tag in the html file. However, I am not working with it in an html file, I would just like to use it in a JavaScript file. Since it is a chrome extension, the JavaScript logic needs to be separate from the HTML code anyways.

For some more background info: I am working in NetBeans, and I have downloaded the entire PapaParse folder into my site root (public_html) folder. I am new to working with libraries, so it's possible I am missing something simple!

MRB
  • 221
  • 2
  • 12
  • Content scripts can't run as modules so you should compile it (e.g. in webpack) into a single bundle. – wOxxOm Aug 27 '20 at 19:58
  • @wOxxOm Thank you! This is the first helpful bit of information I have gotten in the past 10 hours. I am unfamiliar with webpack (and the concept of modules in general), so I am not entirely sure what you mean by this. As far as I understand, PapaParse is the module, not my content script. I am trying to work with the single file papaparse.js, but should I bundle the entire PapaParse library folder? I am trying to import the library PapaParse into my content script. Would creating a separate js file (instead of content script) to handle the csv parsing work instead? – MRB Aug 27 '20 at 20:19
  • If you don't want to spend time on setting up webpack or a similar tool, just don't use the `import` statement, instead add papaparse.min.js before your main content script in "js" list, then use it directly as `Papa`. – wOxxOm Aug 28 '20 at 04:51

0 Answers0