Sub topic 10
Getting the order right: packages and dependencies
This chapter assumes you can read sfdx-project.json, run sf CLI commands against a Dev Hub and an org, and know the difference between a beta and a released package version. It covers the order in which packages must be built, installed, and deployed so that dependencies are always present when they are needed.
Why order matters
A package that depends on another package cannot be created, installed, or deployed unless the package it needs already exists in the right place. The platform checks this at three separate moments, and each check has its own order rule:
- Creating a version. The dependency must already have a package version that satisfies the declared version number.
- Installing a version. Every dependency must already be installed in the target org, at a version equal to or newer than the one required.
- Deploying source. Packages in the project are processed in the order of
packageDirectories, so a dependency must appear above the package that uses it.
Most ordering failures come from mixing these up. A correct packageDirectories order does not help if the dependency was never installed in the target org.
Order in sfdx-project.json
The packageDirectories array is read from top to bottom. A package directory must be listed after every package directory it depends on. In the example below, Utils comes before Sales App because Sales App depends on it.
{
"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"
}
}If you reverse the two entries, source deployment and version creation can fail because components in app refer to components that have not been processed yet. Treat the list as a build order, not a display order.
The default flag is separate from ordering. It marks the directory that commands such as sf project deploy start use when you do not pass --source-dir. Only one directory can be the default.
Order when creating versions
You build each package version on its own, and you build dependencies first. For the project above:
sf package version create --package Utils --installation-key-bypass --wait 20 --code-coverage --target-dev-hub myDevHub
sf package version create --package "Sales App" --installation-key-bypass --wait 20 --code-coverage --target-dev-hub myDevHubWhen Sales App is created, the platform resolves Utils at 1.1.0.LATEST. That keyword means the most recent build of 1.1.0 that exists in the Dev Hub at that moment. If you have not yet created any Utils 1.1.0 version, the second command fails.
Two points about what LATEST can pick:
- When you create a beta version of the dependent package,
LATESTcan resolve to a beta build of the dependency. - When you create a released (promotable) version of the dependent package, the dependency must itself be a released version. Beta versions of a dependency cannot be part of a version you intend to promote.
The second point sets the promotion order: promote Utils first, then Sales App.
sf package version promote --package "[email protected]" --target-dev-hub myDevHub
sf package version promote --package "Sales [email protected]" --target-dev-hub myDevHubIf you promote the dependent first, the command fails because it still points at an unreleased dependency.
Order when installing
Installation follows the same direction: dependencies first. You have two ways to do it.
Install each package yourself
Install the dependency, then the dependent. Use the version alias or the 04t subscriber package version ID.
sf package install --package "[email protected]" --target-org myOrg --wait 10
sf package install --package "Sales [email protected]" --target-org myOrg --wait 10If you skip the first command, the second one stops with an error that names the missing dependency.
Let the CLI check the whole chain
Some commands install dependencies for you. Scratch org creation with sf org create scratch does not install packages, but sf package install report and the project's sf package version create flow can read the dependency list from the project file. The behavior depends on the command and the CLI version, so check the output of --help before relying on it in automation. In a pipeline, installing each package explicitly is the safest choice because the order is visible in the script.
Installing a dependent's dependencies in one step
You can ask the CLI to print the full install order for a package version, including its dependencies, and then use that list in a script:
sf package version report --package "Sales [email protected]" --verbose --target-dev-hub myDevHubThe --verbose output includes the dependency list for that version. Use it to confirm which Utils version the dependent was built against.
Pinned versus floating in the install order
The way you declare a dependency changes which version must be installed first.
| Declaration | What the platform needs installed | Typical use |
|---|---|---|
| `"versionNumber": "1.1.0.LATEST"` | The newest 1.1.0 build at the time you create the dependent version, recorded in that version | Fast-moving packages in one repository |
| `"package": "[email protected]"` | Exactly that build, through its `04t` ID | Stable releases, reproducible installs |
| No `versionNumber` on a same-project dependency | The dependency's version from the current project definition | Not recommended for release builds |
Once the dependent version is created, the dependency is fixed to a concrete version. LATEST is resolved at creation time, not at install time. Installing the dependent later does not pick up newer Utils builds. It requires the build recorded in the dependent version, or any newer one that is compatible in the target org.
Deployment order for source
When you deploy source rather than install packages, for example into a scratch org, the dependency must be available in one of two ways:
- Its source is in a package directory listed earlier in
packageDirectories. - Its package is already installed in the org.
A common scratch org setup script installs the external packages first, then pushes the project source:
sf org create scratch --definition-file config/project-scratch-def.json --alias dev --target-dev-hub myDevHub
sf package install --package "[email protected]" --target-org dev --wait 10
sf project deploy start --target-org devIf the project source already contains Utils, do not also install it as a package. You would have the same components in two places, and the deploy would fail or overwrite the installed copy.
Order across more than two packages
With three or more packages, build a dependency graph and process it from the leaves inward. A package with no dependencies goes first. A package whose dependencies are all processed goes next.
The diagram below shows a chain where Reports needs Sales App, and Sales App needs Utils.
Read the arrow as "must be done before". Apply the same order to creating, promoting, and installing. Circular dependencies are not allowed. If Utils needed something from Reports, you would have to move the shared component into a fourth package that both depend on.
Dependencies that share a base package
Two packages can depend on the same dependency at different versions. Suppose Sales App needs Utils 1.1 and Reports needs Utils 1.0. The org holds only one version of Utils, so it must satisfy both. Installing Utils 1.1 satisfies both requirements only if 1.1 is a newer version in the same package line and the older dependent accepts it. Check that your Reports code does not rely on behavior removed in 1.1.
Checking the order before you ship
Use these checks before you promote or release:
- Confirm that every dependency in
dependenciesappears above the dependent inpackageDirectories. - Confirm that every alias used in a dependency exists in
packageAliases. - Create beta versions in dependency order and install them into a scratch org in the same order.
- Promote in dependency order.
- Write the install order in your release notes or pipeline script.
Common order errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Version creation fails because the dependency version cannot be found | The dependency has no version matching the declared number | Create the dependency version first, or correct the number |
| Promote fails on the dependent | A dependency is still a beta version | Promote the dependency first |
| Install fails naming a missing package | The dependency is not installed in the target org | Install the dependency first |
| Deploy fails with missing component errors | The dependency is listed after the dependent, or not installed | Reorder `packageDirectories` or install the package |
| Alias not found | The dependency is missing from `packageAliases` | Add the alias, or run the package create command to generate it |
Exercise
Your project lists Sales App above Utils in packageDirectories, and Sales App depends on Utils. You also forgot to promote Utils before promoting Sales App. Name the two changes you must make and the order you must do them in.
Show answer
First, move Utils above Sales App in packageDirectories so the dependency is processed first. Then promote the Utils version before you promote the Sales App version, because a version cannot be promoted while its dependency is still a beta.
Exercise
You created Sales App 2.0.0-1 with Utils at 1.1.0.LATEST when the newest build was 1.1.0-2. Later, 1.1.0-3 is created. Which Utils build does the installed Sales App 2.0.0-1 require?
Show answer
It requires the build recorded when the version was created, 1.1.0-2 (or a newer compatible one in the org). LATEST is resolved at creation time, so the later 1.1.0-3 build does not change the requirement.