function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VijayasaharanVijayasaharan 

Lightning web component using third party JS libraries

Hi I am trying to use the XLSX JS libraries to parse a CSV convert to JSON and upload into salesforce object. I had done this in Visual force and wanted to do the same in LWC.
But I am stuck in calling the 3rdd party JS lib method.  My question is how do you call a method in the 3rd lib.
 
import 	shimlatest from '@salesforce/resourceUrl/shimlatest';
import 	dropsh from '@salesforce/resourceUrl/DropSheet';
import 	xlsxfull from '@salesforce/resourceUrl/xlsxfull';

Promise.all([
            loadScript(this, shimlatest),
            loadScript(this, xlsxfull),
        ])
            .then(()  => {
                X = XLSX; / THIS ONE ALWAYS COMES AS undefind 
                console.log('XLSX Value of ' + X);
                if(X !== undefined)
                   X = '';
                   this.initializePage(); 
                })
            .catch(error => {
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error loading XLSX modules',
                        message: error.message,
                        variant: 'error',
                    }),
                );
            });

 
Deepali KulshresthaDeepali Kulshrestha
Hi Vijayasaharan,

We can not directly import the third party scripts in Lightning web components because Security purpose Salesforce restrict the importing scripts from third-party content delivery sites like cdnjs, jsdelivr, etc

1.  First, you need to download the scripts from third-party sites.
2.  Then upload the scripts in static resources.

check below link how to import static resources in Lightning web components
https://www.salesforcecodecrack.com/2019/01/how-to-access-static-resources-in.html
and for syntax check out the given link :
https://www.salesforcecodecrack.com/2019/03/how-to-import-third-party-libraries-in.html

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Jian Ming YuJian Ming Yu
Hi Vijayasaharan

I have got the same error message during using XLSX, have you got the solution ? If so please advise thanks
Ankit PurohitAnkit Purohit
If you want to use 3rd party JavaScript libraries in your LWC components, store those files as Static Resource and then call that Static Resource in your component. Due to security, you cannot directly use the JS library in LWC. Hope it helps you.