Skip to main content

Create a Hello World Lightning Web Component

Follow Along with Trail Together

Want to follow along with an expert as you work through this step? Take a look at this video, part of the Trail Together series on Trailhead Live.

(This clip starts at the 11:49 minute mark, in case you want to rewind and watch the beginning of the step again.)

Create a Salesforce DX Project

Now that you’ve set up your development environment, you can create a simple Lightning web component.

  1. In Visual Studio Code, open the Command Palette by pressing Ctrl+Shift+P (Windows) or Cmd+Shift+P (macOS).
  2. Type SFDX.
  3. Select SFDX: Create Project.
  4. Press Enter to accept the standard option.
  5. Enter HelloWorldLightningWebComponent as the project name.
  6. Press Enter.
  7. Select a folder to store the project.
  8. Click Create Project. You should see something like this as your base setup.
    Visual Studio Code with the newly created HelloWorldLightningWebComponent folder

Note

If you get a permissions prompt ‘Do you trust the authors of the files in this folder?’, select Yes.

Authorize Your Trailhead Playground

  1. In Visual Studio Code, open the Command Palette by pressing Ctrl+Shift+P (Windows) or Cmd+Shift+P (macOS).
  2. Type SFDX.
  3. Select SFDX: Authorize an Org.
  4. Press Enter to accept the Project Default login URL option.
  5. Press Enter to accept the default alias.
    This opens the Salesforce login in a separate browser window.
  6. Log in using your Trailhead Playground credentials.
  7. If prompted to allow access, click Allow.
    Allow Access dialog
  8. After you authenticate in the browser, the CLI remembers your credentials. The success message should look like this:
    Success message of authorizing an org

Create a Lightning Web Component

  1. In Visual Studio Code, open the Command Palette by pressing Ctrl+Shift+P (Windows) or Cmd+Shift+P (macOS).
  2. Type SFDX.
  3. Select SFDX: Create Lightning Web Component.
  4. Enter helloWorld for the name of the new component.
  5. Press Enter to accept the default force-app/main/default/lwc.
  6. Press Enter.
  7. View the newly created files in Visual Studio Code.
    Lightning web component file hierarchy in Visual Studio Code
  8. In the HTML file, helloWorld.html, copy and paste the following code.

    <template>
      <lightning-card title="HelloWorld" icon-name="custom:custom14">
        <div class="slds-m-around_medium">
          <p>Hello, {greeting}!</p>
          <lightning-input label="Name" value={greeting} onchange={changeHandler}></lightning-input>
        </div>
      </lightning-card>
    </template>
  9. Save the file.
  10. In the JavaScript file, helloWorld.js, copy and paste the following code.

    import { LightningElement } from 'lwc';
    export default class HelloWorld extends LightningElement {
            greeting = 'World';
            changeHandler(event) {
            this.greeting = event.target.value;
            }
    }
  11. Save the file.
  12. In the XML file helloWorld.js-meta.xml, copy and paste the following code.

    <?xml version="1.0" encoding="UTF-8"?>
    <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="helloWorld">
        <apiVersion>58.0</apiVersion>
        <isExposed>true</isExposed>
        <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
        </targets>
    </LightningComponentBundle>
  13. Save the file.

Deploy to Your Trailhead Playground

  1. Right-click the default folder under force-app/main.
    With the default folder right-clicked, SFDX: Deploy Source to Org is selected in the options list.
  2. Click SFDX: Deploy Source to Org.
  3. In the Output tab of the integrated terminal, view the results of your deployment. If the command ran successfully, a Deployed Source message lists the three files that were uploaded to the org.

Add Component to App in Lightning Experience

  1. In Visual Studio Code, open the Command Palette by pressing Ctrl+Shift+P (Windows) or Cmd+Shift+P (macOS).
  2. Type SFDX.
  3. Select SFDX: Open Default Org.
    This opens your Trailhead Playground in a separate browser.
  4. Click Setupthen select Setup.
  5. In Quick Find, enter Home, then select Home in the Feature Settings section.
  6. For Advanced Seller Home, toggle the setting to Inactive.
  7. From the App Launcher (), find and select Sales.
  8. Click Setupthen select Edit Page.
  9. Drag the helloWorld Lightning web component from the Custom area of the Lightning Components list to the top of the Page Canvas. 
    Lightning App Builder with HelloWorld lightning web component on the right column.
  10. Click Save.
  11. Click Activate.
  12. Click Assign as Org Default.
  13. Click Save.
  14. Click Save again, then click Back to return to the page.
  15. Refresh the page to view your new component.

Home page with the HelloWorld Lightning web component.

You’ve officially made your first Lightning web component!

What’s next?

Check out the code samples and SDK’s on https://developer.salesforce.com/code-samples-and-sdks. Add more to your helloWorld component, check out the other sample components, and build your own components! As you experiment, use the Component Reference to learn more about how to code Lightning web components.

Keep learning for
free!
Sign up for an account to continue.
What’s in it for you?
  • Get personalized recommendations for your career goals
  • Practice your skills with hands-on challenges and quizzes
  • Track and share your progress with employers
  • Connect to mentorship and career opportunities