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

**Topic:** Development · **Difficulty:** Medium · **Tags:** lwc, static-resources, third-party

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-you-use-a-third-party-javascript-library-or-static-resource-in-lwc

## Answer

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).

---
Source: ForceChallenges · last updated 2026-10-19
