Use Static Resource In Lightning Web Components

As a Salesforce developer we know how important Static resource are. We can upload and can refer them using CDN also. But it is always suggested to import third party library into salesforce static resource. If you still didn’t started Lightning Web Components ( LWC ) you can do that here. Today we will check how we can use Static Resource In Lightning Web Component. Due to Lightning Web Components content security policy requirement we need to upload our external files in static resource first. So today we will learn how we can use static resource in LWC and we will also check how it is different then lightning.

For demo purpose I have upload a basic zip file which include one CSS file, jQuery and two images.

 Static Resource in Lightning Web Components ( LWC ) and in Lightning


        <img src={customImage}/>

        <div class="lwcClass">I'm in Lightning Web Components ( LWC )</div>


import { LightningElement } from 'lwc';
import { loadScript, loadStyle } from 'lightning/platformResourceLoader';
import customSR from '@salesforce/resourceUrl/customSR';

export default class LwcStaticResource extends LightningElement {
    customImage = customSR + '/LwcDemo.jpg';  

    renderedCallback() {

            loadScript(this, customSR + '/jquery.min.js'),
            loadStyle(this, customSR + '/customCss.css'),
            .then(() => {
                alert('Files loaded.');
            .catch(error => {

In Lightning Web Components ( LWC ) we first need to import loadScript, loadStyle to load the static resource file. There syntax is loadStyle(self, fileUrl): where in self we need to pass this and in URL we need to pass the URL of static resource file. For image we can direct access URL. In case of multiple files we can do this.

    loadScript(this, resourceName + '/lib1.js'),
    loadScript(this, resourceName + '/lib2.js'),
    loadScript(this, resourceName + '/lib3.js')
]).then(() => { /* callback */ });

If you have directly uploaded your JS, CSS files in static resource without making a zip then you need to refer them using below code

import jquery from '@salesforce/resourceUrl/jquery';
import bootstrap from '@salesforce/resourceUrl/bootstrap';

        loadScript(this, jquery),
        loadStyle(this, bootstrap),
  ]).then(() => { /* callback */ });

Now to achieve similar result in Lightning we need to do below things.


    <ltng:require styles="{!$Resource.customSR +'/customCss.css'}"  
        scripts="{!$Resource.customSR +'/jquery.min.js'}" 
        afterScriptsLoaded="{!c.setup}" />

        <img src="{!$Resource.customSR + '/lightningDemo.jpg'}"/>
        <div class="lightningClass">I'm in Lightning</div>

    <c:lwcStaticResource />


    setup : function(component, event, helper) {
        if (typeof jQuery != 'undefined') {
            alert('jquery loaded in lightning.');

As we can see in Lightning resource are loaded in component while in LWC we load them in controller and then use it. So in many area LWC is totally different then Lightning.

Do you want to add anything here or have any question, let me know in comments. Happy programming 🙂


16 thoughts on “Use Static Resource In Lightning Web Components

      1. import { LightningElement } from ‘lwc’;

        import {loadStyle} from ‘lightning/platformResourceLoader’;

        import green from ‘@salesforce/resourceUrl/green’;
        import style from ‘@salesforce/resourceUrl/style’;
        import bootstrap from ‘@salesforce/resourceUrl/bootstrap’;

        export default class eventRequest extends LightningElement {

        renderedCallback() {

        loadStyle(this, green + ‘/green.css’),
        .then(() =>




        I have three files green.css, style.css, and bootstrap.min.css. I have uploaded to Static Resource like green,style, bootstrap. pls find the above code. The styles are not applied.

        1. It looks like you have directly added the files in static resource instead of zip, So you need to change your code
          loadStyle(this, green ),

  1. $(function () {
    showPeriod: true,
    showLeadingZero: true

    // enable/disable the time fields based on an all day Meeting
    $(“.allday”).change(function (event) {
    if ($(‘.allday’).is(‘:checked’)) {
    else {

    How do I render the Date /Time picker Jquery/Bootstrap CSS through script in LWC ?

    1. I don’t think you can doirectly call apex. But you can call JSController method and from there can call the Apex. I have never tried this in LWC so don’t have any sample code for you.

  2. how to call a method of the loaded script function in the javascript?Ex: if I have loaded a script named ‘abc’. Now I want to call a method of the abc. Let the method be fgh. How do I call it in LWC?

  3. Hi Tushar, thank you for the tips in your post. I was able to get an image displayed in a lightning web component by following your example but now I need to display an SVG file. I can get the SVG to show up as an image, but the links in it do not work. Have you been able to display an SVG file in a lightning web component and have links in the SVG be clickable?


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.