Use Static Resource In LWC(Lightning Web Component)

Use Static Resource In LWC(Lightning Web Component)

Static Resource In LWC

First, Import static resources from the @salesforce/resourceUrl scoped module. Static resources can be archives (such as .zip and .jar files), images, style sheets, JavaScript, and other files.

The syntax for importing static resource in LWC

import myResource from '@salesforce/resourceUrl/resourceReference';

When static resource has namespace

import myResource from '@salesforce/resourceUrl/namespace__resourceReference';
  • myResource—A name that refers to the static resource.
  • resourceReference—The name of the static resource.A static resource name can contain only underscores and alphanumeric characters, and must be unique in your org. It must begin with a letter, not include spaces, not end with an underscore, and not contain two consecutive underscores.
  • namespace—If the static resource is in a managed package, this value is the namespace of the managed package.


Use Static Resource In Lightning Web Component Example

We will create LWC component to show images. We will get the following output

Use Static Resource In LWC


Let’s create static resource with two images, css file and javascript files.

First, create myresource zip file with the following content. It contains images, css and js folder.

myResource content

Go to Setup -> Static resource -> Create New

Create static resource



    <lightning-card title="Static Resource Example LWC" icon-name="custom:custom19">
        <div class="slds-m-around_medium">
            <img src={spring20Logo}>
            <img src={summer20Logo}>


import { LightningElement } from 'lwc';
import My_Resource from '@salesforce/resourceUrl/myResource';
export default class StaticResourceLWCExample extends LightningElement {
    spring20Logo = My_Resource + '/images/salesforce-spring-2020.jpg';
    summer20Logo = My_Resource + '/images/salesfoce-summer-2020.jpeg';


<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="">

Now we can add this lwc component on home page.

  • Go to Home page
  • Click Setup (Gear Icon) and select Edit Page.
  • Under Custom Components, find your staticResourceLWCExample component and drag it on right hand side top.
  • Click Save and activate.

For more details please refer to official link

Permanent link to this article:

1 comment

    • Selvin on May 27, 2020 at 3:59 pm
    • Reply

    can we use static resource image in lightning treegrid?

Leave a Reply

Your email address will not be published.