简体   繁体   中英

How do I import PapaParse (or any js library) into a JavaScript File?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM