Sub topic 7
sfdx-project.json and package directories
This chapter assumes you know the difference between Metadata API format and source format from the earlier chapters. It also assumes you can read JSON and run commands in a terminal.
What the file does
sfdx-project.json sits in the project root. Its presence marks the directory as a source-format project. The Salesforce CLI searches the current directory and its parents for this file. If it finds none, source-format commands such as sf project deploy start --source-dir fail.
Metadata API format has no equivalent file. A package.xml lists what to deploy, but nothing tells the tooling where your source lives. sfdx-project.json supplies that.
A minimal file
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"namespace": "",
"sourceApiVersion": "60.0"
}| Key | Purpose |
|---|---|
| `packageDirectories` | The folders the CLI treats as source. |
| `path` | A folder path relative to the project root. It is required for each entry. |
| `default` | Marks the folder that receives new components. |
| `namespace` | The namespace prefix for a managed package. Leave it empty if you have none. |
| `sourceApiVersion` | The API version the CLI uses when it deploys, retrieves, or converts. |
sourceApiVersion plays the role that <version> plays in package.xml. When you convert source to Metadata API format, the CLI writes this value into the generated manifest.
Package directories
A package directory is a folder whose contents the CLI reads as source format. Everything outside the listed folders is invisible to source commands. A file placed in a folder that is not listed will not be deployed.
You can list several directories. This is common when you split a project into modules:
{
"packageDirectories": [
{ "path": "core", "default": true },
{ "path": "sales" },
{ "path": "service" }
],
"sourceApiVersion": "60.0"
}Follow these rules:
- Exactly one entry has
"default": truewhen you list more than one. - The CLI reads the directories in the order you list them. Put a directory before any directory that depends on it.
- Do not put the same component in two directories. Two copies of
Account/fields/Rating_Notes__c.field-meta.xmlmake it unclear which one is authoritative. - Use forward slashes in paths on every operating system.
Inside each directory the layout is free. force-app/main/default is only the convention that sf project generate creates. The CLI finds components by their folder names and suffixes, not by how deep they sit.
How the file affects conversion
When you run sf project convert source, the CLI uses this file in two ways. Without --root-dir, it converts every listed package directory. With --root-dir, it converts only that folder, which should be one of your package directories. It also takes the API version for the generated package.xml from sourceApiVersion.
sf project convert source --root-dir core --output-dir mdapi_coreThe converse direction, convert mdapi, writes into the directory you name. It does not need the file to read the input, but you should list the output folder in packageDirectories before you use it as a project source.
Common errors
- "This directory is not a valid Salesforce project." You ran a command outside the project tree, or the file is missing or misnamed. Check the spelling and your working directory.
- A component is missing from a deploy. Its folder is not in
packageDirectories. Add the path. - The JSON fails to parse. A trailing comma is the usual cause. The file must be strict JSON.
Exercise
Your project has force-app and shared-utils folders, but only force-app is listed in packageDirectories. You run sf project convert source with no --root-dir. Are the Apex classes in shared-utils in the output? What do you change?
Show answer
No. The CLI converts only listed package directories, so shared-utils is ignored. Add { "path": "shared-utils" } to packageDirectories, keeping exactly one entry marked default, and place it after any directory it depends on. Then rerun the conversion.