Sub topic 4
What sfdx-project.json does and how the CLI finds it
This chapter assumes you have the Salesforce CLI (sf) installed, can read JSON, and know the basic Salesforce metadata types such as Apex classes and Lightning web components. You should also have generated a project once with sf project generate.
The role of the file
sfdx-project.json is the project configuration file. It sits in the root folder of a Salesforce DX project. Its presence is what makes a folder a DX project as far as the CLI is concerned.
The file answers four questions for the CLI:
- Where is the source code? The
packageDirectoriesarray lists the folders. - Which API version should source operations use? The
sourceApiVersionsetting says. - Which login URL should be used when you authorize an org? The
sfdcLoginUrlsetting says. - Is this a namespaced or packaged project? The
namespace,package, andpackageAliasessettings say.
Here is the file that sf project generate creates:
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}The file does not contain your metadata. It does not store org credentials either. Those live in your user-level CLI configuration, outside the project. The project file only describes the project's structure and settings, so it is safe to commit to version control and share with a team.
How the CLI finds the file
Many sf commands need to know about the project. Examples are sf project deploy start, sf project retrieve start, and sf org create scratch. The CLI does not ask you for the project path. It looks for sfdx-project.json starting at your current working directory. If the file is not there, the CLI checks the parent folder, then the parent of that folder, and so on up to the root of the file system. The first sfdx-project.json it finds defines the project.
This has practical effects:
- You can run project commands from any subfolder of the project, such as
force-app/main/default/classes, and they still work. - If you run a project command from outside any project, the CLI cannot find the file and reports an error. Typically the message says that the command must run inside a Salesforce DX project.
- If you nest one project inside another, the closest file wins. The CLI uses the inner project when you are inside it.
The lookup walks upward only. The CLI never searches child folders. If your repository has the project in a subfolder, such as app/sfdx-project.json, you must cd app before running project commands.
Commands that need the file and commands that do not
Not every command requires a project. The distinction helps when you see a "no project found" error.
| Command | Needs sfdx-project.json? | Why |
|---|---|---|
| `sf project generate` | No | It creates the file. |
| `sf org login web` | No | Authorization is stored at user level. The login URL can be passed with `--instance-url`. |
| `sf org list` | No | It reads stored org data, not project data. |
| `sf project deploy start` | Yes | It needs the package directories to know what to deploy. |
| `sf project retrieve start` | Yes | It needs to know where to write retrieved source. |
| `sf org create scratch` | Usually | It reads the project for namespace and tracking details, and you pass the definition file explicitly. |
Treat the last row as a rule of thumb. If a command touches your local source, it needs the project file.
Inspecting what the CLI sees
You can confirm which file the CLI is using without opening an editor. From any folder inside the project, run:
sf project generate --name demo
cd demo/force-app/main/default
sf project deploy start --dry-runThe first line creates a project. The second line moves into a deep subfolder. The third line works even though sfdx-project.json is three levels up, because of the upward search. The --dry-run flag asks the CLI to validate the deploy without saving changes. It still needs an org to talk to. If you have not set a default org, pass --target-org with an alias or username.
To see the problem the other way, move out of the project and try the same command:
cd ../../../..
cd /tmp
sf project deploy start --dry-runThe CLI cannot find a project from /tmp, so it stops with an error before contacting any org.
Settings at a glance
The remaining chapters cover each setting in detail. This table is a map so you know where each one fits.
| Setting | Level | Purpose |
|---|---|---|
| `packageDirectories` | Project | Lists the source folders. Required. |
| `path` | Directory | The folder path, relative to the project root. |
| `default` | Directory | Marks the folder that receives new source. |
| `package`, `versionName`, `versionNumber` | Directory | Declare an unlocked package. |
| `dependencies` | Directory | Lists packages this directory needs. |
| `name` | Project | A label for the project. |
| `namespace` | Project | The namespace for a managed or namespaced package. Empty string if none. |
| `sfdcLoginUrl` | Project | The default login URL for org authorization. |
| `sourceApiVersion` | Project | The API version used for source operations. |
| `packageAliases` | Project | Maps package names to IDs. |
Project-level settings apply to the whole project. Directory-level settings belong inside one object in the packageDirectories array.
Reading the file as the CLI does
When a command runs, the CLI reads the file, parses it as JSON, and validates it. Understanding this order explains most errors.
- The CLI parses the text. A trailing comma, a missing quote, or a comment makes the file invalid JSON. JSON does not allow comments.
- The CLI checks the structure.
packageDirectoriesmust exist and be an array. - The CLI checks the values. Paths must point to real folders, and exactly one directory should be marked as default when there are several.
- The command runs with the resolved settings.
A parsing failure is the most common problem after a hand edit. Typical causes are a comma after the last property in an object and single quotes around strings. Fix those first before you suspect anything else.
Editing the file safely
You can edit the file by hand, and most teams do. A few habits reduce errors:
- Use an editor with JSON validation. VS Code with the Salesforce extensions flags syntax problems as you type.
- Change one thing at a time, then run a quick command such as
sf project deploy start --dry-run. A failure points to your last change. - Commit the file to version control. A diff shows exactly what changed when something stops working.
- Keep paths relative to the project root, and use forward slashes. Both work on macOS, Linux, and Windows.
Project file and other configuration
New users often confuse sfdx-project.json with other configuration. Each has a different scope.
| Configuration | Location | Scope | Example contents |
|---|---|---|---|
| `sfdx-project.json` | Project root | The project, shared through Git | Package directories, API version |
| `.sf/config.json` | Inside the project | Local settings for this project | Default target org |
| Global config | User home folder | All projects for this user | Global default org |
| `.forceignore` | Project root | The project | Files to exclude from deploys |
| Scratch org definition | `config/` folder | One org shape | Edition, features, settings |
The local and global configuration files are written by sf config set. They hold personal choices, such as which org is your default. They should not be committed to Git. The project file holds choices the whole team shares.
sf config set target-org my-scratch
sf config get target-orgThe first command stores a default org for the current project. The second prints the stored value. Neither changes sfdx-project.json.
Common mistakes
- Running a command from the wrong folder. The CLI cannot find the file. Run
pwdand confirm you are inside the project. - Having two project files. A nested copy silently takes precedence over the outer one. Search with
find . -name sfdx-project.jsonif behavior surprises you. - Putting personal values, such as a default username, in the project file. Use
sf config setinstead. - Editing the file with a tool that adds comments or trailing commas.
Exercise
You run sf project deploy start from /home/dev/repo/docs. The project file is at /home/dev/repo/app/sfdx-project.json. The command fails because no project is found. Why does the lookup fail, and what is the fix?
Show answer
The CLI searches the current folder and then its parents: /home/dev/repo/docs, /home/dev/repo, /home/dev, and so on. It never searches sibling or child folders, so it never reaches /home/dev/repo/app. The fix is to run cd /home/dev/repo/app (or any folder under it) and rerun the command.
Exercise
Your teammate adds the line // legacy folder inside sfdx-project.json and every project command now fails. What is the cause, and where should the note go instead?
Show answer
JSON does not support comments, so the CLI fails at the parsing step before it checks any settings. Remove the comment. Put the note in a README or in the commit message.