Getting Started with LWC

Lightning Web Components is an open-source UI framework developed at Salesforce. You can use LWC to customize Salesforce, but you can also use LWC to build any web app. The LWC compiler and engine code available in the open-source repository is ahead of the code available on the Salesforce Platform. Most, but not all, LWC features are eventually released on the Salesforce Platform.

Getting started with LWC

There are many ways for getting started with LWC, from the online playground to a fully custom build process to integration into an existing project.

Online playground

The easiest way to get started with LWC is to use an online playground like

Via create-lwc-app

To create a brand new Lightning Web Components project, use create-lwc-app.

The tool guides you through a short setup and scaffold a Lightning Web Components project. To use the tool, you must have npx, which is a package runner.

npx create-lwc-app my-app
cd my-app

npm run watch       # Start local server with file watcher
npm run build       # Build application in production mode

Via rollup

If you need more control to build your application or if you need to integrate LWC with an existing project, the recommended way is via rollup. In this section, we discuss the minimal setup to create an LWC application using rollup.

To get started, install the following packages first.

npm install --save-dev lwc rollup @lwc/rollup-plugin @rollup/plugin-replace

Add a rollup.config.js at the root of the repository. The rollup configuration file indicates to rollup the application entry point, the output and the desired plugins to apply.

import lwc from "@lwc/rollup-plugin";
import replace from "@rollup/plugin-replace";

export default {
    input: "src/main.js",

    output: {
        file: "dist/main.js",
        format: "esm",

    plugins: [
            "process.env.NODE_ENV": JSON.stringify("development"),

Add a lwc.config.json file at the root of the repository to instruct the LWC compiler how to resolve your LWC modules.

    "modules": [
            "dir": "src/modules"

Now that we are done with the configuration, it’s time to create the application entry point. You can add the following content to src/main.js:

import { createElement } from "lwc";
import App from "x/app";

const elm = createElement("x-app", { is: App });

The entry point imports the x/app LWC component. As configured in the lwc.config.json, all the LWC modules should be resolved from src/modules.

To create the x/app component, add the following files: src/modules/x/app/app.js and src/modules/x/app/app.html:

import { LightningElement } from 'lwc';

export default class App extends LightningElement {}
    <h1>Hello world</h1>

With everything in place, you can invoke rollup to compile the application by running rollup --config rollup.config.js. Rollup outputs the compiled application code in dist/main.js.

Use Lightning Web Components as Native Web Components

To register a Lightning web component as a custom element, use LightningElement.CustomElementConstructor, which is a static getter that can be accessed by any constructor that extends LightningElement.

In this example, the component name is component, and the namespace is namespace.

import NamespaceComponent from 'namespace/component';
customElements.define('namespace-component', NamespaceComponent.CustomElementConstructor);
// using tagName <namespace-component>

To package and distribute your component, see Module Resolution.

Tools used to develop Lightning Web Components

To develop Lightning web components, you can use just about any code editor and tools.

For code formatting, we recommend Prettier. Prettier supports HTML, CSS, and JavaScript, which are the files you write to create Lightning web components.

To install and use Prettier, see the official documentation. To configure Prettier, add a configuration file to your project. To correctly format HTML templates with Prettier, set the parser to lwc. The parser is just HTML, but it tells Prettier not to add quotes around template properties in HTML attributes as required by LWC.

The following example sets all HTML files to use the lwc parser.

    "overrides": [
            "files": "*.html",
            "options": { "parser": "lwc" }


The simplest recipe is the helloWorld component. The name property in the component’s JavaScript class binds to the component’s HTML template. Change World to Earth to see the binding in action.


import { LightningElement, api } from 'lwc';

export default class Example extends LightningElement {
    @api name = 'World!';


    Hello, {name}


Hello, World!


(function (lwc) {
    'use strict';

    function tmpl($api, $cmp, $slotset, $ctx) {
      const {
        t: api_text,
        d: api_dynamic
      } = $api;
      return [api_text("Hello, "), api_dynamic($];

    var _tmpl = lwc.registerTemplate(tmpl);
    tmpl.stylesheets = [];
    tmpl.stylesheetTokens = {
      hostAttribute: "example-helloWorld_helloWorld-host",
      shadowAttribute: "example-helloWorld_helloWorld"

    class Example extends lwc.LightningElement {
      constructor(...args) {
        super(...args); = 'World!';


    lwc.registerDecorators(Example, {
      publicProps: {
        name: {
          config: 0

    var Ctor = lwc.registerComponent(Example, {
      tmpl: _tmpl

    const element = lwc.createElement('repl-output', {
      is: Ctor


Add another property in helloWorld.js.

@api greeting = 'Welcome to Lightning Web Components!'

Don’t forget to add {greeting} in the helloWorld.html template.

The @api decorator makes the name property public. Because the name and greeting properties are public, a component that consumes the helloWorld component can set their values.

If we remove @api, the property still binds to the HTML template but it’s private. To see for yourself, remove @api.

To learn more, see HTML Templates.

Supported Browsers

Microsoft® EdgeLatest
Google Chrome™Latest
Mozilla® Firefox®Latest
Apple® Safari®Latest

Supported JavaScript

To develop Lightning web components, use the latest versions of JavaScript. You can use any JavaScript feature that the browser supports.

The Salesforce engineers who developed Lightning Web Components are contributing members of the Ecma International Technical Committee 39 (TC39), which is the committee that evolves JavaScript. Salesforce is also a member of the World Wide Web Consortium (W3C).

This developer guide explains how to develop Lightning web components and documents the directives, decorators, and lifecycle hooks that are unique to LWC.

This developer guide doesn’t document standard JavaScript or teach JavaScript fundamentals. Standard JavaScript is documented in the Mozilla Developer Network (MDN) JavaScript Reference. If you’re looking for documentation for a function, try MDN first. For example, if you’re looking for information about addEventListener(), use MDN.

Scroll to Top