Sub topic 7
Generating a Salesforce DX project
This chapter assumes you have the Salesforce CLI (sf) installed and a Dev Hub already authorized as my-hub. It also assumes you can read basic JSON. Here you create the local project that holds your source code and your scratch org definition.
What a Salesforce DX project is
A Salesforce DX project is a local folder with a fixed layout and a configuration file named sfdx-project.json. The CLI reads that file to find your source code and to decide which API version to use. You usually keep the folder in version control, so the project, not any one org, is the source of truth for your code.
Generate the project
Run the generator from the folder where you want the new project to live.
sf project generate --name my-project
cd my-projectThe command creates a folder called my-project and fills it with starter files. The main ones are:
| Path | Purpose |
|---|---|
| `sfdx-project.json` | Project settings: package directories, namespace, login URL, and API version |
| `config/project-scratch-def.json` | The scratch org definition file, which you edit in the next chapter |
| `force-app/main/default/` | The default folder for your metadata source |
| `.forceignore` | Patterns for files the CLI must not deploy or retrieve |
| `manifest/` | Sample `package.xml` for manifest-based deploys |
The --name value is the project name and the folder name. If you want the project to go somewhere else, add --output-dir with a path.
Read sfdx-project.json
Open sfdx-project.json. A generated file looks like this, though the API version will match your installed CLI.
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}Each entry in packageDirectories names a folder the CLI treats as source. Exactly one entry must be marked "default": true. Commands such as sf project deploy start search these folders when they look for changes.
Locate the scratch org definition file
The generator also writes config/project-scratch-def.json. This file tells Salesforce how to build a scratch org. The command that creates the org points at this file with --definition-file, so it must be reachable from where you run the command.
The diagram shows how the pieces relate. The project supplies the definition file, and the Dev Hub uses it to create the scratch org.
Common errors
- Running commands outside the project. Commands that read
sfdx-project.jsonfail if your terminal is not inside the project folder. Runcd my-projectfirst. - Nesting a project inside another project. Generate a new project in a clean location, not inside an existing DX project.
- Forgetting
default: true. If no package directory is marked as default, deploy and retrieve commands report a project configuration error.
Check your work
List the generated files to confirm the layout.
ls config force-app/main/defaultYou should see project-scratch-def.json under config. If the file is missing, you ran the command in a different folder than you expected.
Exercise
Which file tells the CLI where your source code folders are, and which setting marks the folder used by default?
Show answer
sfdx-project.json lists them in packageDirectories. The entry with "default": true is the default folder.
Exercise
You run sf project generate --name my-project and then, without changing folders, run a deploy command. It fails to find a project. What is the likely cause and the fix?
Show answer
The terminal is still in the parent folder, not inside my-project. Run cd my-project and run the command again.