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.
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.
Creating, Deploying, and Retrieving Components Quiz
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.