Bundle npm packages into minified static resources
.js or .zip filePublicNote: Static Resource names are case-sensitive and cannot contain spaces or special characters.
Reference the Static Resource in your Visualforce page:
<apex:page>
<apex:includeScript value="{!$Resource.myPackage}"/>
<script>
// Use the library (e.g., lodash)
const result = _.map([1, 2, 3], n => n * 2);
</script>
</apex:page>
Replace myPackage with your Static Resource name. If you downloaded a zip, reference the file inside: {!$Resource.myPackage}/filename.min.js
Import the Static Resource in your LWC JavaScript file:
import { loadScript } from 'lightning/platformResourceLoader';
import myPackage from '@salesforce/resourceUrl/myPackage';
export default class MyComponent extends LightningElement {
connectedCallback() {
loadScript(this, myPackage)
.then(() => {
// Library is now available
console.log('Library loaded:', window.myPackage);
})
.catch(error => {
console.error('Error loading library:', error);
});
}
}
The library will be available on the window object using the global variable name defined during bundling.
Load the Static Resource in your Aura component:
<aura:component>
<ltng:require scripts="{!$Resource.myPackage}"
afterScriptsLoaded="{!c.scriptsLoaded}"/>
<div aura:id="container">
<!-- Your component markup -->
</div>
</aura:component>
In your controller:
({
scriptsLoaded: function(component, event, helper) {
// Library is now available
console.log('Library loaded:', window.myPackage);
}
})
window.lodash or window.myPackage).