Sub topic 3
Describing packages in sfdx-project.json
This chapter assumes you know Salesforce DX projects, the sf CLI, Dev Hubs, and how to read JSON. It covers the file that tells the CLI what each package contains and how to version it.
What the file does
The sfdx-project.json file sits at the root of a Salesforce DX project. For packaging, it answers three questions: which folders belong to which package, what version each package is on, and which ID each package name maps to. The CLI reads it every time you create a package version.
The file has four packaging parts:
packageDirectoriesis a list of folders. Each entry can describe a package.namespaceis the namespace that packages in this project use. Leave it empty for unlocked packages without a namespace.sfdcLoginUrlis the login URL for orgs.packageAliasesmaps readable names to Salesforce IDs.
A working example
{
"packageDirectories": [
{
"path": "util",
"package": "Utils",
"versionName": "Utils 1.1",
"versionNumber": "1.1.0.NEXT",
"default": false
},
{
"path": "app",
"package": "Sales App",
"versionName": "Sales App 2.0",
"versionNumber": "2.0.0.NEXT",
"default": true,
"dependencies": [
{ "package": "Utils", "versionNumber": "1.1.0.LATEST" }
]
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"Utils": "0Ho000000000001AAA",
"Sales App": "0Ho000000000002AAA"
}
}Fields in a package directory
| Field | Meaning |
|---|---|
| `path` | Folder that holds the package's source. |
| `package` | Package name. It must match an alias or a package ID. |
| `versionName` | A label for people, such as "Utils 1.1". |
| `versionNumber` | The `MAJOR.MINOR.PATCH.BUILD` number. `NEXT` asks for the next build number. |
| `default` | Marks the directory the CLI uses when you don't name one. Exactly one entry is `true`. |
| `dependencies` | Other packages this one needs. The next chapters cover this. |
How packageAliases connects names to IDs
A package ID starts with 0Ho. A package version ID starts with 04t. Neither is easy to remember, so packageAliases maps a name to the ID. The package field in a directory uses that name.
When you run sf package create, the CLI adds the new alias for you. You rarely write these IDs by hand. After you create a version, the CLI also adds an alias such as [email protected] that points to the 04t ID of that version.
sf package create --name "Utils" --package-type Unlocked --path util --no-namespace --target-dev-hub myDevHubThe --path value must match the path of a directory in the file. The package then points at that folder.
Order matters
The CLI builds and installs packages in the order of the list in packageDirectories. In the example, Utils comes before Sales App because Sales App depends on it. A package must appear before anything that depends on it.
Common mistakes
- Two entries with
"default": true, or none, which makes the project invalid or the default unclear. - A
paththat doesn't match a real folder. - Listing a dependent package before the package it needs.
- Setting
namespacefor an unlocked package that should have none, which ties the package to that namespace.
Exercise
In the example file, which entry is used when you run a package command without naming a directory, and what does the 0Ho value under packageAliases identify?
Show answer
The app entry (Sales App) is used because it has "default": true. A 0Ho value is a package ID. The alias lets you refer to the package by name.