Use Lightning Web Components in Visualforce

Use Lightning Web Components in Visualforce

Use Lightning Web Components in Visualforce

In this post, we will see how we can use LWC in Visualforce Pages or how to display Lightning Web Component In Visualforce Page. Even though the use of lightning is growing day by day, still some customers are using salesforce classic because of many reasons. Sometimes there is a requirement to use lightning web component in visualforce page.

Lightning Web Components for Visualforce is based on Lightning Out, a powerful and flexible feature that lets you embed Lightning web components into almost any web page. When used with Visualforce, some of the details become simpler. For example, you don’t need to deal with authentication, and you don’t need to configure a Connected App. If you are looking for using lightning aura components in visualforce pages, please refer to Use Lightning Components in Visualforce Pages.

In this example, we will see a very basic example of the lightning web component and then display lightning web component in the visualforce page.


    <lightning-card title="Use LWC in Visualforce" icon-name="custom:custom19">
        <div class="slds-m-around_medium">
            Hello, This is LWC for visualforce page


import { LightningElement } from 'lwc';
export default class HelloComponentForVFLWC extends LightningElement {}

Here important point is that ltng:outApp needs to be extended in app.

ltng:outApp adds SLDS resources to the page to allow our Lightning components to be styled with the Salesforce Lightning Design System. If we don’t want SLDS resources added to the page, we need to extend from ltng:outAppUnstyled instead.

<aura:application extends="ltng:outApp" access="GLOBAL">
    <aura:dependency resource="helloComponentForVFLWC" />

<apex:page showHeader="false" sidebar="false">
    <apex:includeLightning />    
    <div id="LightningComponentid" />    
    $Lightning.use("c:HelloLWCExampleApp", function() {
          function(cmp) {
             console.log('LWC Componenet added in VF page');

In the above Visualforce page, apex:includeLightning tag imports necessary dependencies and scripts to enable Visualforce to act as Lightning component container.

We can use $Lightning.use() method in JavaScript to refer to Lightning Application which extends ltng:outApp.

We can use $Lightning.createComponent create Lightning Component dynamically.

We can call $Lightning.use() multiple times on a page, but all calls must reference the same Lightning dependency app.

We will get the following output

Use Lightning Web Components in Visualforce

For more detail please refer to official link

Permanent link to this article:


Skip to comment form

    • wej on May 19, 2020 at 8:28 pm
    • Reply

    Hi Ankush,
    I did the same thing as you, with LWC I used ( record-edit-form) + (lightning-input-field) because I need to get record id after the creation to use it in (lightning-file-upload) the form works very well in my organization, but with VF the form didn’t return the record id, and the upload file didn’t work too.

    1. As per Salesforce documentation file upload component does not work with lightning out. Here is statement
      “This component is not supported in Lightning Out or standalone apps, and displays as a disabled input.”
      You can find it in official link from my post

    • Tillu on June 1, 2020 at 2:07 pm
    • Reply

    HI Ankush Dureja,

    I’m new to lightning web Component. while using Lightning web Component In visual force page . I got an error msg like “An internal server error has occurred” can you please help me with that .

      • Akash on June 18, 2020 at 11:38 pm
      • Reply

      Yes I got the same error

      1. Can you please share code?

          • Tillu on July 26, 2020 at 2:45 pm
          • Reply

          I copied the same code that you have mentioned in this site

  1. Excellent Content .thank you

    • rayyan on June 17, 2020 at 4:23 pm
    • Reply

    Can we do vice versa of it like showing/embed vf page inside LWC component

    • Aman Anil Tiwari on July 9, 2020 at 5:34 pm
    • Reply

    Tysm for this informative content!!

    • Pauline on November 6, 2020 at 6:36 pm
    • Reply

    Hi Ankush. I used your code on a blank vf page and my component works fine. However, when I insert the same code on an existing VF page with other content the component is not rendered. Any idea why?

    I have tried inserting the div in different parts of my page but only the other vf content is being rendered.

    Please help.

    • Manish Das on February 20, 2021 at 2:41 am
    • Reply

    HI Ankush,

    I am using LWC Record Edit form inside a VF page . After clicking on the save button of LWC record page then I am using the Id of the new record created to navigate using Navigation Mix. But on the Save Handler event , Record ID is not returned . Please help me . I am pasting the Handle Success Function:

    handleSuccess(event) {
    if(this.redirect == true){
    let creditnoteId =;
    console.log(‘creditnoteId’+this.redcreditnoteIdirect); /// – Here I am not getting the Record ID
    type: ‘standard__recordPage’,
    attributes: {

    • Yugandhar on May 19, 2021 at 2:25 pm
    • Reply

    check before posting the code here. Same error i also got.

    1. This code is still working for me. Can you please share your code.

    • amit on August 6, 2021 at 12:38 am
    • Reply

    hi ankush,
    when i try this with my app and component file its running normally but not showing anything on console

    • Prasan on September 27, 2021 at 10:47 am
    • Reply

    Hi ankush,

    I’m getting the below error and I’m searching for a way to resolve it. Could you please help me?

    Thank You,
    Prasan De Alwis

Leave a Reply

Your email address will not be published.