Sub topic 1
What a Salesforce DX Project Is
Overview
A Salesforce DX project is a directory of files that holds your Salesforce customizations as text. It also holds a configuration file named sfdx-project.json. The Salesforce CLI (sf) reads that file to learn where your source lives, which API version to use, and which packages the project defines.
The idea behind the project is source-driven development. In older workflows, the org was the source of truth. You changed things in a sandbox, then moved them with change sets. In a DX project, version control is the source of truth. The org is a deployment target that you can rebuild from the files.
This chapter explains what a DX project is, what it contains, and how the CLI finds and uses it. Later chapters cover each file and setting in detail.
Prerequisites
This chapter assumes you have:
- Basic Salesforce concepts: orgs, metadata, Apex classes, and Lightning web components.
- The Salesforce CLI (
sf) installed, and at least one org authorized. A Dev Hub org is needed later for scratch orgs. - Comfort with a terminal and with Git basics such as
clone,commit, andstatus. - Enough JSON knowledge to read objects, arrays, and strings, and to spot a missing comma.
You can check the CLI with sf --version. If the command prints a version, you are ready.
Why the project exists
Classic Salesforce development had several problems that the project format addresses.
- No reliable history. Changes made in the org's Setup pages leave no readable diff. Version control can record only what you export.
- Hard collaboration. Two developers working in one shared sandbox can overwrite each other's work.
- Manual promotion. Change sets are assembled by hand and cannot be scripted or reviewed in a pull request.
- Hard to reproduce. Rebuilding a working org for testing meant a lot of clicking.
A DX project turns your customizations into files. Files can be reviewed, branched, merged, tested in continuous integration, and deployed by a script. The same project can feed a scratch org, a sandbox, or production.
The parts of a DX project
A DX project is a convention, not a single artifact. Four things make it up.
| Part | What it is | Purpose |
|---|---|---|
| `sfdx-project.json` | JSON file at the project root | Tells the CLI where source is and how the project is configured |
| Package directories | Folders such as `force-app` | Hold your metadata as source files |
| `.forceignore` | Gitignore-style pattern file | Keeps matching files out of deploys and retrieves |
| `config/project-scratch-def.json` | JSON file | Describes the shape of scratch orgs for this project |
Around these sit ordinary repository files such as .gitignore, a scripts folder, and often tooling configuration for Prettier, ESLint, and Jest.
Only sfdx-project.json is strictly required for the CLI to treat a directory as a project. The other parts are conventions that the default template creates for you and that other commands expect.
Generating a project
The quickest way to see a DX project is to generate one. The command below creates a project from the default template, then lists the result, including hidden files.
sf project generate --name my-project
cd my-project
ls -aThe --name flag sets the folder name and also the name value written into sfdx-project.json. The command also accepts a --template flag. The default is standard. Use empty for a smaller skeleton, or analytics for Tableau CRM projects.
The generated layout looks like this:
my-project/
├── .forceignore
├── .gitignore
├── config/
│ └── project-scratch-def.json
├── force-app/
│ └── main/
│ └── default/
│ ├── classes/
│ ├── lwc/
│ └── objects/
├── scripts/
└── sfdx-project.jsonYour version may list additional files, such as package.json and editor settings, depending on the CLI version. Those extras do not change how the project works.
What each top-level item does
sfdx-project.jsonis the project manifest. The next section shows its minimal form.force-app/is the default package directory. Your metadata goes underforce-app/main/default, in folders named for each metadata type.config/project-scratch-def.jsondefines the edition, features, and settings of scratch orgs you create from this project..forceignorelists files the CLI should skip.scripts/holds helper scripts, for example Apex scripts that load sample data..gitignorekeeps local files such as dependencies and CLI state out of Git.
The project file in brief
Here is the default sfdx-project.json for the generated project.
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}Read it as answers to four questions the CLI would otherwise have to guess:
- Where is the source?
packageDirectoriespoints toforce-app. - Which folder gets new files by default? The entry marked
"default": true. - Which login endpoint should be used?
sfdcLoginUrl. - Which metadata API version applies to source operations?
sourceApiVersion.
Each of these gets its own chapter. For now, remember that the file is small, and the CLI trusts it completely.
How the CLI finds the project
Many sf commands work only inside a project. When you run one, the CLI looks for sfdx-project.json in the current directory. If it does not find the file, it checks the parent directory, and keeps going up until it reaches the filesystem root.
This has practical effects:
- You can run commands from any subfolder of the project, such as
force-app/main/default/classes. - A command run outside any project fails with a message that no
sfdx-project.jsonwas found. - A stray
sfdx-project.jsonin a parent folder, for example your home directory, can make an unrelated folder behave as a project. Keep that file only in real project roots.
Commands that work without a project include sf org login web and sf org list, because they deal with org authorization, not source.
How a project connects to orgs
The project holds source. Orgs hold the running configuration. The CLI moves source between them.
A typical loop looks like this:
- Create a scratch org from the definition file in
config. - Deploy the project source to it.
- Change code or configuration, in your editor or in the org.
- Retrieve any org-side changes back into the project.
- Commit to Git and open a pull request.
The commands for steps 1, 2, and 4 are:
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --set-default
sf project deploy start
sf project retrieve startRun the last two from inside the project. Both read packageDirectories to decide which folders to send or receive. In scratch orgs, and in sandboxes with source tracking enabled, the CLI tracks changes on both sides. Then deploy start and retrieve start with no extra flags move only what changed. Production and sandboxes without tracking need explicit flags such as --source-dir or --metadata.
Source format and metadata format
Metadata API deployments traditionally use a single bundle: one large XML file per type, listed in a package.xml manifest. This is called metadata format. A DX project instead stores source format. Source format breaks large components into smaller files so that Git diffs are readable and merge conflicts are rarer. For example, a custom object is stored as one folder holding a file per field, rather than one huge object file.
You do not need to convert files by hand. The CLI converts between the two forms during deploy and retrieve. A later chapter covers the mapping in detail.
Common misunderstandings
- "The project is the org." It is not. The project is a set of files. Deploying is what makes an org match it.
- "Everything in the org belongs in the project." Often untrue. You commit only the metadata your team owns, and
.forceignorehelps exclude the rest. - "
sfdx-project.jsonconfigures my login." It only sets the login URL used for authorization. Credentials are stored by the CLI outside the project. - "The folder name must be
force-app." It need not be. It is only the default name. Any folder listed inpackageDirectoriesworks.
Summary of key points
- A DX project is a source-controlled directory that the CLI understands through
sfdx-project.json. - Version control, not the org, is the source of truth.
sf project generatecreates the standard layout, withforce-appas the default package directory.- The CLI finds the project by searching the current directory and then each parent.
- Deploy and retrieve commands use the project's configuration to move source to and from orgs.
Exercise
You run sf project deploy start from my-project/force-app/main/default/classes, which is three levels below the folder containing sfdx-project.json. Will the CLI recognize the project? Explain how it finds the file, and name one problem that could occur if another sfdx-project.json exists in a higher parent folder.
Show answer
Yes. The CLI starts in the current directory and searches each parent directory until it finds sfdx-project.json, so it locates the file at the project root. It uses the nearest match found while walking up. If a different sfdx-project.json sits in a higher folder, it matters only when no closer file exists. In that case a non-project folder can be treated as part of that unrelated project, and commands might read the wrong package directories.
Exercise
Name the four main parts of a DX project from the table in this chapter, and state which one is the only strictly required file for the CLI to treat a directory as a project.
Show answer
The parts are sfdx-project.json, the package directories such as force-app, .forceignore, and config/project-scratch-def.json. Only sfdx-project.json is strictly required.