Sub topic 7
sfdx-project.json and Package Directories
This chapter assumes you can read JSON and XML, you know what an Apex class, custom object, and custom field are, and you can run basic commands in a terminal. It covers the file that makes a directory a source-format project and the settings that tell the Salesforce CLI where your source lives.
What sfdx-project.json does
sfdx-project.json is the project configuration file for a Salesforce DX project. It sits in the project root. The Salesforce CLI looks for it when you run commands such as sf project deploy start or sf project convert source. Without it, the CLI does not treat the directory as a source-format project.
The Metadata API format has no equivalent. A Metadata API format directory is described by package.xml, which lists what to move. sfdx-project.json describes something different: where the source lives on disk, which API version to use, and how the project is packaged.
| File | Format | Answers the question |
|---|---|---|
| `package.xml` | Metadata API format | Which components should this retrieve or deploy include? |
| `sfdx-project.json` | Source format | Where does the project's source live, and how is it configured? |
A minimal file looks like this:
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"namespace": "",
"sourceApiVersion": "60.0"
}Three top-level keys appear here.
packageDirectoriesis a list of folders that hold source. It is the only required key.namespaceis the namespace prefix for the project. An empty string means no namespace.sourceApiVersionis the Metadata API version the CLI uses when it retrieves, deploys, or converts source.
Package directories
A package directory is a folder, relative to the project root, that the CLI treats as source. The folder you see most often is force-app, which is the default that project generation creates. Inside it, the standard layout is force-app/main/default/, and below that you find folders such as classes, objects, and lwc.
Each entry in packageDirectories has these common keys.
| Key | Meaning |
|---|---|
| `path` | Folder that holds source, relative to the project root. Required. |
| `default` | Set to `true` on exactly one entry. The CLI writes newly retrieved source here when it cannot place it elsewhere. |
| `package` | Name of a package, used when the directory is built into a managed or unlocked package. |
| `versionNumber` | Version of the package, such as `1.0.0.NEXT`. |
| `versionName` | Human-readable name for that version. |
| `ancestorId` or `ancestorVersion` | The earlier package version this one builds on. |
For ordinary deployment and version control work you need only path and default. The package-related keys matter once you create unlocked or second-generation managed packages, which is outside this chapter.
Rules the CLI enforces
- The path must exist on disk, or commands that read it fail.
- Exactly one directory should be marked
default: truewhen you have more than one entry. If you have a single entry, the CLI treats it as the default. - Paths use forward slashes, even on Windows.
- The directories must not overlap. A folder cannot appear inside another package directory.
Multiple package directories
A project can have several package directories. A common reason is to split code by team or by deployment unit, for example a core layer and a feature layer.
{
"packageDirectories": [
{ "path": "core", "default": true },
{ "path": "feature-sales", "default": false },
{ "path": "unpackaged", "default": false }
],
"namespace": "",
"sourceApiVersion": "60.0"
}The order matters. When the CLI deploys or converts, it processes directories in the order they are listed. If two directories contain the same component, such as Account.object-meta.xml in both, the later one wins in some operations and the CLI may warn about the duplicate. Avoid defining the same component in more than one directory.
When you retrieve source, the CLI looks for a file with a matching name in any package directory and updates it in place. A component that exists nowhere locally goes into the default directory. This is why a clear default matters.
How the CLI reads the file
The diagram shows what happens when you run a source-format command.
Notice the .forceignore step. The project file decides which folders are scanned, and .forceignore then removes files inside them. A later chapter covers that file.
The sourceApiVersion setting
sourceApiVersion pins the Metadata API version. It plays the same role as the <version> element in package.xml, which the CLI uses for Metadata API format.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>MyClass</members>
<name>ApexClass</name>
</types>
<types>
<members>Account</members>
<name>CustomObject</name>
</types>
<version>60.0</version>
</Package>Here the <version>60.0</version> line is the manifest's version. In a source-format project, "sourceApiVersion": "60.0" in sfdx-project.json controls the same thing.
Why this matters:
- The API version decides which metadata types and fields exist. A type introduced in a later release is unknown at an earlier version.
- The CLI uses it when it generates files, so the same project can produce different XML at different versions.
- The version also affects the
<apiVersion>value the CLI writes into the-meta.xmlfile of new Apex classes.
If sourceApiVersion is missing, the CLI falls back to the API version of the org it connects to. That fallback can make results differ between developers, so it is better to set the value explicitly and update it on purpose.
Where the project file affects conversion
Conversion is the most direct place to see sfdx-project.json at work. When you convert from source format to Metadata API format, the CLI reads the project file to find your source, then writes the output as a flat directory with a generated package.xml.
# Source format -> Metadata API format
sf project convert source --root-dir force-app --output-dir mdapi_output
# Metadata API format -> source format
sf project convert mdapi --root-dir mdapi_output --output-dir force-app
# Deploy a Metadata API format directory
sf project deploy start --metadata-dir mdapi_output --wait 10Two details are easy to miss.
sf project convert sourcemust run inside a project, because it needssfdx-project.jsonto resolve--root-dir. The reverse command,sf project convert mdapi, does not read a Metadata API directory through the project file, but you still run it from a project so the output lands in a known package directory.--root-dirshould point to one of your package directories, or to a folder inside one. If you pass a folder outside any package directory, the CLI cannot map the files to components and reports an error.
Editing the file safely
The file is plain JSON, so a syntax error breaks every command. Common mistakes:
- A trailing comma after the last item in a list or object.
- A path with a backslash, as in
force-app\main. - Two entries both marked
default: true. - A
paththat points to a folder you have not created yet.
A quick way to catch syntax errors is to parse the file before you commit:
python3 -m json.tool sfdx-project.jsonIf the file is valid, the command prints the formatted JSON. If it is not, it reports the line and column of the error.
Where namespace fits
namespace is empty for most projects. Set it only when you develop a namespaced package. With a namespace set, the CLI treats custom components as belonging to that prefix, which changes how names appear in deployed metadata. If you set a namespace on a project that does not own one, deployments can fail because the namespace does not exist in the target org. When in doubt, leave it as an empty string.
Summary of the key points
sfdx-project.jsonmarks a directory as a source-format project.package.xmldoes the matching job for Metadata API format, but it describes contents, not location.packageDirectorieslists the folders the CLI scans. Mark one entry as the default.sourceApiVersionfixes the API version, and you should set it explicitly.- Conversion and deployment commands read this file to find your source.
- Valid JSON and non-overlapping directories prevent most project-level errors.
Exercise
A project has "packageDirectories": [{ "path": "core" }, { "path": "extras" }], and you retrieve a new custom field that exists in neither folder. Which problem can occur, and how do you fix the configuration?
Show answer
The CLI has no folder marked as the default, so it cannot decide where to write the new field (with several entries, exactly one should have "default": true). Fix it by setting "default": true on one entry, for example { "path": "core", "default": true }.
Exercise
You run sf project convert source --root-dir scripts and the CLI reports an error, although scripts contains files. What is the most likely cause?
Show answer
The scripts folder is not inside any package directory listed in sfdx-project.json, so the CLI cannot map its files to metadata components. Point --root-dir at a package directory such as force-app, or add the folder to packageDirectories.