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 have generated a project with sf project generate. You should also know what metadata is and what an org is.
The role of the file
sfdx-project.json marks a directory as a Salesforce DX project. It sits at the project root and tells the CLI three things:
- Where your source code lives (
packageDirectories). - Which settings apply to the whole project, such as
sourceApiVersion,sfdcLoginUrl, andnamespace. - Which packages the project builds and how they relate (
packageAliasesand package fields).
The CLI does not store this information anywhere else. Commands like sf project deploy start and sf project retrieve start read the file each time they run. If the file is wrong, those commands fail or act on the wrong folders.
How the CLI finds the file
The CLI looks for sfdx-project.json in the directory where you run the command. If it is not there, it checks the parent directory, then that directory's parent, and so on up to the filesystem root. The first match defines the project root.
This means you can run project commands from any subfolder:
cd my-project/force-app/main/default/classes
sf project deploy startThe command above deploys the whole project, not just the classes folder. The CLI walks up to my-project/, finds the file, and resolves every path from there.
What a project-aware command does with it
Some commands need the project and some do not. sf org list works anywhere. sf project deploy start needs the file, because it must know which folders to read. Outside a project, you get an error such as "This command is required to run from within a Salesforce project directory."
Here is a minimal file:
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}packageDirectories is the only field the CLI treats as essential for source commands. The others have defaults or matter only for specific tasks, which later chapters cover.
Paths are relative to the project root
Every path value is relative to the folder that contains sfdx-project.json. Write force-app, not ./force-app or an absolute path. A path that does not exist on disk causes an error when you deploy or retrieve.
Common errors
- Invalid JSON. A trailing comma or a missing quote makes the whole file unreadable. The CLI reports a parse error and runs nothing. Check the file in an editor that highlights JSON syntax.
- Wrong working directory. If you see the "must run from within a project" message, you are outside the project tree. Change into the project folder.
- A second
sfdx-project.jsonhigher up. Because the CLI stops at the first file it finds, a stray file in a nested folder can hide the real one. Runpwdand check which file is nearest.
Exercise
You run sf project deploy start from my-project/force-app/main/default/lwc. The file my-project/sfdx-project.json exists. Does the command work, and what does it deploy?
Show answer
It works. The CLI searches upward from the current directory, finds sfdx-project.json in my-project, and uses that as the project root. It deploys the source in the package directories listed in the file, not only the lwc folder.
Exercise
A teammate adds a stray sfdx-project.json inside my-project/force-app. What problem can this cause when you run commands from force-app/main/default?
Show answer
The CLI finds the nearest file first, so it treats force-app as the project root instead of my-project. Paths and package directories are resolved against the wrong folder, which can cause path errors or wrong deploys.