Sub topic 4
Defining packages in sfdx-project.json
This chapter covers how sfdx-project.json describes your packages, and how the Salesforce CLI uses that file. It assumes you know basic Salesforce metadata (objects, fields, Apex classes, permission sets). It also assumes you can use scratch orgs and the Salesforce CLI, and that you keep your source in Git. The chapter does not create a package. It only describes one. The next chapters create the package record in the Dev Hub and build versions.
What the file does
sfdx-project.json sits at the root of a Salesforce DX project. It tells the CLI four things:
- Which folders contain source code.
- Which of those folders are packages.
- What version each package is working toward.
- Which other packages each one needs.
Package versions are built from the folders you list here. A folder that is not listed is not part of any package version. Metadata outside every package is "unpackaged" and is deployed by other means.
A two-package example
The following file defines two packages. Core holds shared metadata. Sales depends on Core.
{
"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"
}Project-level keys
These keys sit at the top level of the file and apply to the whole project.
| Key | Meaning |
|---|---|
| `packageDirectories` | An array of folder definitions. This is the only key that is always required. |
| `namespace` | The namespace used when packages are created. An empty string means no namespace. |
| `sfdcLoginUrl` | The login URL for authorizing orgs. Use `https://login.salesforce.com` for production and Dev Hub orgs. |
| `sourceApiVersion` | The API version used when the CLI interprets and deploys your source. |
| `packageAliases` | A map of friendly names to Salesforce IDs. The CLI writes this key for you. |
Package directory keys
Each entry in packageDirectories describes one folder. These keys matter for packages.
| Key | Meaning |
|---|---|
| `path` | The folder holding the source, relative to the project root. The folder must exist. |
| `package` | The package name or alias this folder builds into. |
| `versionName` | A human-readable label for the version, such as "Spring release". |
| `versionNumber` | The version being built, in the form `major.minor.patch.build`. |
| `default` | Marks the folder the CLI uses by default when you retrieve or deploy source. Exactly one folder should be `true`. |
| `dependencies` | The other packages this one needs. |
Package and versionNumber
The package value must match a package that exists in your Dev Hub. The CLI looks up the matching ID in packageAliases. If the name is not there, version creation fails.
1.0.0.NEXT means "major 1, minor 0, patch 0, and the next build number". You write NEXT instead of a number, and the platform assigns the next free build number when you create a version. The following chapter covers version numbers in detail.
Dependencies
A dependency names another package and the version you need. 1.0.0.LATEST asks for the latest build of Core version 1.0.0. You can also pin a specific version by using an alias such as [email protected] in place of the package and versionNumber pair.
Order matters
The CLI processes packageDirectories from top to bottom. A package must be listed before any package that depends on it. In the example, Core comes first, so it is available when Sales is built. If you reverse the order, version creation for Sales fails because its dependency is not yet defined.
The same ordering rule applies to the order in which packages are installed in an org. You install Core before Sales.
How packageAliases appears
When you create a package, the CLI adds a packageAliases section to the file. You do not write it by hand. After creating both packages and one version of each, it looks like this:
{
"packageAliases": {
"Core": "0HoXXXXXXXXXXXXXXX",
"[email protected]": "04tXXXXXXXXXXXXXXX",
"Sales": "0HoXXXXXXXXXXXXXXX"
}
}IDs starting with 0Ho identify the package. IDs starting with 04t identify a specific package version. The alias [email protected] means version 1.0.0, build 1. You use these aliases in commands such as sf package install --package "[email protected]".
Commit packageAliases to Git. Without it, a teammate or a CI job cannot resolve package names to IDs.
Creating the package record
The file only describes a package. The package record lives in the Dev Hub. This command creates it and writes the alias entry:
sf package create --name Core --package-type Unlocked --path force-app/core --no-namespace --target-dev-hub MyDevHubThe --path value must match a path in sfdx-project.json. The --name value becomes the package key. Run the equivalent command for Sales after Core.
Common errors
- Folder does not exist. A
paththat points to a missing folder stops every command. Create the folder first, even if it is empty. - No default folder, or two. Retrieve and deploy commands behave unpredictably. Set
default: trueon exactly one entry. - Dependency listed after its dependent. Reorder the array so dependencies come first.
- Package name not in
packageAliases. The package record was never created, or the alias file was not committed. Runsf package createor restore the file from Git. - Dependency on a version that does not exist.
1.0.0.LATESTfails if noCore1.0.0 build exists yet. BuildCorefirst. - Namespace mismatch. A package created with
--no-namespacecannot be defined while the top-levelnamespaceis set to a value, and the reverse. Keep them consistent. - Circular dependencies. If
CoreneedsSalesandSalesneedsCore, no valid order exists. Move the shared metadata into a third, lower-level package.
Checking your file
Before building anything, check these points:
- Every
pathexists on disk. - Exactly one entry has
default: true. - Each
packagename has a matching Dev Hub record. - Each dependency appears earlier in the array than the package that needs it.
- The top-level
namespacematches how you created the packages.
Exercise
A teammate clones your repository and runs a version build for Sales. The build fails because the CLI cannot find the Core dependency. Name two likely causes in sfdx-project.json.
Show answer
The first cause is that Core is listed after Sales in packageDirectories, so it is not yet defined when Sales is processed. The second is that packageAliases is missing or was not committed, so the CLI cannot resolve Core to a package ID. A third possibility is that no Core 1.0.0 version has been built yet, so 1.0.0.LATEST has nothing to match.
Exercise
What is the difference between the IDs that start with 0Ho and 04t in packageAliases?
Show answer
An ID starting with 0Ho identifies a package record in the Dev Hub. An ID starting with 04t identifies one specific package version, such as [email protected].