Beginner 18 min read

Creating, Deploying, and Retrieving Components

By the end of this lesson, you'll be able to:

  • Generate a new LWC component using the CLI
  • Deploy a component to an org
  • Retrieve existing metadata changes back from an org

Prerequisites: "Scratch Orgs vs. Sandboxes"

Generating a New Component

sf lightning generate component --name myComponent --type lwc --output-dir force-app/main/default/lwc

This scaffolds the .html/.js/.js-meta.xml file set automatically — the same file set Module 1's Hello Salesforce project built by hand, to understand the structure firsthand. From here on, you'll usually let the CLI generate the skeleton.

Deploying to an Org

sf project deploy start --source-dir force-app/main/default/lwc/myComponent

This pushes your local source to the target org. Being specific with --source-dir deploys only what you intend to change — faster and lower-risk than deploying an entire project folder for a one-component change.

Retrieving from an Org

sf project retrieve start --source-dir force-app/main/default/lwc/myComponent

The reverse direction: pulls the current org version down into your local project. Essential when someone changed something directly in the org (through a Setup UI, for instance) that your local files don't yet reflect.

Source Tracking in Scratch Orgs

Scratch orgs uniquely support automatic source tracking: the CLI knows what's changed locally vs. in the org, so sf project deploy start and sf project retrieve start can run with no --source-dir at all and sync only the actual differences. Sandboxes and production have no such tracking — you are always explicit about exactly what you deploy or retrieve there.

Exercise

Write the command to generate a new LWC named "accountBadge" into the standard force-app lwc folder.

Show hint

Use sf lightning generate component with --type lwc.

BASH

Exercise

Challenge: an admin changes one of your deployed component's configurable properties directly in Lightning App Builder, in your scratch org. Explain what command you'd run before continuing to code, and why.

Show hint

Think about which direction pulls org changes back to your machine.

BASH

Creating, Deploying, and Retrieving Components Quiz

1. What does sf lightning generate component do?

2. Which command pulls an org's current state DOWN into your local project?

3. Automatic source tracking (no --source-dir needed) is available for sandboxes and production, not just scratch orgs.

4. Why is it generally better to deploy with a specific --source-dir rather than the entire force-app folder for a one-component change?

5. An admin changes a component's config directly in App Builder. What should a developer do before their next deploy?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

The CLI's generate, deploy, and retrieve commands form the core loop of local LWC development — and scratch orgs uniquely add automatic source tracking on top of that loop.