Sub topic 7
Create a Salesforce DX project
This chapter assumes you have installed the sf CLI and can run sf --version. It also assumes you know what Salesforce metadata is. You do not need to be logged in to an org to create a project. You do need an org later, when you deploy.
A Salesforce DX project is a local folder that holds your org's metadata as source files. The folder is built around a file named sfdx-project.json. The CLI reads that file to learn where your source lives and which API version to use.
Generate the project
Run sf project generate with a name. The CLI creates a folder with that name and fills it with a default layout.
sf project generate --name myproject
cd myprojectThe command is made of the topic project and the command generate. The --name flag sets the project folder name.
What the project contains
The default layout looks like this:
myproject/
├── config/
│ └── project-scratch-def.json
├── force-app/
│ └── main/
│ └── default/
│ ├── classes/
│ ├── lwc/
│ ├── aura/
│ └── objects/
├── scripts/
├── .forceignore
├── .gitignore
├── package.json
└── sfdx-project.jsonThe files and folders you will use most often are these:
| Path | Purpose |
|---|---|
| `sfdx-project.json` | Project configuration. Marks the folder as a DX project. |
| `force-app/` | The default package directory. Your source goes here. |
| `config/project-scratch-def.json` | Definition file for creating scratch orgs. |
| `.forceignore` | Lists files the CLI skips during deploy and retrieve. |
| `scripts/` | A place for your own scripts, such as sample Apex. |
The exact contents of the generated folder can vary a little between CLI versions. Check your own output rather than relying on this list.
Read sfdx-project.json
Open sfdx-project.json. A newly generated project has content similar to this:
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "myproject",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}The version number in your file will differ. These fields matter most:
packageDirectoriesis a list of folders that contain source. The CLI only deploys and retrieves from these folders.pathis the folder name, relative to the project root.defaultmarks the folder the CLI uses when you create new files without naming a location. Exactly one directory should be the default.sourceApiVersionis the metadata API version used when the CLI deploys and retrieves source.sfdcLoginUrlis the login page used bysf org login web.
You can add more package directories to organize code into separate folders. Each one needs its own path entry.
flowchart TDHow the project connects to later steps
The project is the link between your local files and an org. Later chapters use these parts of it.
Common errors
Most problems at this stage come from running a command in the wrong place.
- Running project commands outside the project. Commands such as
sf project deploy startlook forsfdx-project.jsonin the current folder or a parent. If you see an error that the file cannot be found, runpwdandcdinto the project. - Editing the wrong folder. Source placed outside a folder listed in
packageDirectoriesis ignored by deploy and retrieve. - Creating the project inside another project. Nested projects confuse the CLI about which
sfdx-project.jsonapplies. Generate new projects in a plain parent folder. - Setting a very old
sourceApiVersion. Metadata types added in newer versions will not be handled correctly.
Check your work
List the folder and print the config file to confirm the project exists.
ls
cat sfdx-project.jsonYou should see force-app and sfdx-project.json. The next chapter uses this project to deploy and retrieve metadata.
Exercise
A teammate runs sf project deploy start and gets an error that no project file can be found. What is the most likely cause, and what should they check first?
Show answer
They are probably running the command outside the DX project, so the CLI cannot find sfdx-project.json in the current folder or any parent. They should check their location with pwd and then cd into the project folder that contains sfdx-project.json.
Exercise
You add a folder named extras next to force-app and put an Apex class in it. Deploys ignore the class. What change to sfdx-project.json fixes this?
Show answer
Add an entry for extras to the packageDirectories list, for example { "path": "extras" }. Only folders listed there are deployed and retrieved.