Today, lets learn.. how to create, update, retrieve and delete a lightning web component(lwc) from an sfdx project using command line interface (CLI).
So, lets start from scratch..
Go to a fresh / New folder in your desktop.. and execute the below command.
sfdx force:project:create --projectname dreamhouse
It creates an empty sfdx project container with name dreamhouse, now point your cmd to project home..
Now, set / link
- Any of your production org ( as Default-DevHub Org) and
- A scratch-Org(as Default-Scratch Org) to your project
Config – Production :
The command will redirect you to login page of salesforce, So provide the credentials and set that particular org as default devHub
sfdx force:auth:web:login --instanceurl https://login.salesforce.com --setdefaultdevhubusername
Config – Default Scratch Org :
Please follow the above steps with slight change in command
sfdx force:auth:web:login --instanceurl https://test.salesforce.com --setdefaultusername
Now lets start with retrieve… if you already have a lightning web component(lwc) deployed in any org (your sandbox, production or scratch orgs).
To retrieve a particular lightning web component(lwc) into your local folder(sfdx project) execute the below command.. in your project home
sfdx force:source:retrieve -m LightningComponentBundle:helloBinding -u email@example.com
NOTE : For -u parameter in above command, you can provide any user from production or sandbox or scratch-org, but we have make sure that, already we have connected / linked that user and that particular org with this project.. otherwise force:config:set username error will be thrown..
if we want to deploy the component that we retrieved just now after making some changes in it., then we can use the below command to deploy into any org..
sfdx force:source:deploy -m LightningComponentBundle:helloBinding -u firstname.lastname@example.org
NOTE : whenever we use retrieve command.. It overwrites the existing lightning web component in sfdx project, It do not try to merge the code and whenever we use deploy command.. It overwrites the existing lightning web component in sfdc org, It do not try to merge the code.
To create a lightning web component use the below command in project’s home
sfdx force:lightning:component:create --type lwc -n helloWorld -d force-app/main/default/lwc
To delete a lightning web component from any org use the below command in project’s home, and the component will be deleted from both local project folder and sfdc org
sfdx force:source:delete -m LightningComponentBundle:helloBinding -u email@example.com
if component is referred in any of the lightning page, error will be thrown as below, otherwise component will be deleted successfully..
Now, we tried to delete a component that is not referred in any of the lightning page