Development questions

How do you use a third-party JavaScript library or static resource in LWC?

DevelopmentMedium
  1. Upload the library as a Static Resource (e.g. chartjs).
  2. Import the resource and loaders:
javascript
import CHARTJS from '@salesforce/resourceUrl/chartjs';
import { loadScript, loadStyle } from 'lightning/platformResourceLoader';

renderedCallback() {
  if (this.loaded) return;
  this.loaded = true;
  loadScript(this, CHARTJS)
    .then(() => this.initChart())
    .catch(e => console.error(e));
}

loadScript/loadStyle return promises; load once, guard with a flag, and the library must be compatible with Lightning Web Security / Locker. Never load scripts from external CDNs (CSP blocks them unless allow-listed in CSP Trusted Sites).

lwcstatic-resourcesthird-party