Sub topic 4
Defining packages in sfdx-project.json
This chapter assumes you can work with scratch orgs and the Salesforce CLI, and that you know what a Dev Hub is from the previous chapter. The sfdx-project.json file at the project root tells the CLI which folders are packages, how they are versioned, and what they depend on.
Package directories
The packageDirectories array is the core of the file. Each entry maps a folder of source to a package. Folders that are not in the array are not part of any package.
{
"packageDirectories": [
{
"path": "force-app/core",
"package": "Core",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"default": true
},
{
"path": "force-app/sales",
"package": "Sales",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"dependencies": [
{ "package": "Core", "versionNumber": "1.0.0.LATEST" }
]
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}Key properties
| Key | Purpose |
|---|---|
| `path` | Folder that holds the package's source. |
| `package` | Package name, which must match the package record in the Dev Hub. |
| `versionName` | Human-readable label for the version. |
| `versionNumber` | Version in `major.minor.patch.build` form. `NEXT` lets the Dev Hub assign the build number. |
| `default` | Marks the folder that source commands use by default. Exactly one entry has this set to `true`. |
| `dependencies` | Other packages that must be installed first. |
| `namespace` | Top-level key. An empty string means no namespace. |
Order and dependencies
The order of the array matters. List a package after every package it depends on, so that Core comes before Sales. The CLI uses this order when it deploys directories to a scratch org.
Dependencies use the package name and a version number. 1.0.0.LATEST selects the newest build of Core 1.0.0 at the time Sales is built. Once Sales has been built, that dependency is fixed to a specific Core version.
What the CLI adds later
You write the directory entries first. When you run sf package create, the CLI adds a packageAliases section to the file. It maps names such as Core to package IDs that start with 0Ho. After you build versions, it adds aliases such as [email protected], which map to version IDs that start with 04t. Commit these changes. CI jobs and teammates need them to resolve the aliases.
Common errors
- Package not found. The
packagevalue is spelled differently from the Dev Hub record, or the package was never created. - Path does not exist. The
pathfolder must exist and contain source before you build a version. - Wrong dependency order. A dependent package is listed before the package it needs, so the scratch org deploy fails.
- Invalid JSON. The file does not allow comments or trailing commas.
Exercise
In the file above, why is Core listed before Sales, and what does "1.0.0.LATEST" mean in Sales' dependency?
Show answer
Sales depends on Core, so Core must be installed or deployed first, and the array order gives that sequence. "1.0.0.LATEST" tells the CLI to use the newest build of Core version 1.0.0 when it builds Sales. The build then records the specific Core version it used.