Sub topic 13
Dependencies between packages and directory order
This chapter assumes you can read sfdx-project.json, know what a package directory is, and have seen the package, versionName, and versionNumber fields. Here you will see how one package declares that it needs another, and why the order of entries in packageDirectories matters.
Declaring a dependency
A package directory lists what it needs in a dependencies array. Each entry names a package and, usually, a version.
{
"path": "feature",
"package": "feature-pkg",
"versionName": "Initial version",
"versionNumber": "0.1.0.NEXT",
"default": false,
"dependencies": [
{ "package": "core-pkg", "versionNumber": "1.0.0.LATEST" }
]
}The package value is a name that must exist as a key in packageAliases. The versionNumber has the form major.minor.patch.build. LATEST in the build position means "the newest build of this version that exists." When you create a package version, the CLI resolves LATEST to a concrete build and records it.
A dependency can point to a package in the same project, like core-pkg here. It can also point to a package from another source. For an external package, add an alias that maps a readable name to a package version ID, which starts with 04t. Then reference that alias in dependencies.
Directory order matters
The CLI reads packageDirectories from top to bottom. A package must appear before every package that depends on it. In the two-package example, core comes first and feature second. If you swapped them, feature would reference a package the CLI has not yet processed.
The same order is used when you deploy the whole project. Source in earlier directories is deployed first, so components that later directories reference already exist in the org.
The solid arrow shows listing order. The dotted arrow shows the dependency, which points back to the earlier entry. A dependency pointing to a later entry is the mistake to avoid.
Order and the default directory
Order does not decide which directory is the default. The "default": true flag does that. In the example, core is both first and the default. You could list core first and mark feature as the default. That is valid, and the CLI would use feature as the target for new source.
Only one directory can be marked default.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Error that a dependency is not found | The name in `dependencies` has no matching key in `packageAliases` | Add the alias or correct the spelling |
| Error about dependency order | The depended-on package is listed after the package that needs it | Move it higher in `packageDirectories` |
| Version cannot be resolved | `versionNumber` asks for a build that does not exist | Create the package version first, or lower the requested version |
| Circular dependency | Two packages depend on each other | Move shared source into a third, lower package |
> Circular dependencies are impossible to order, because each package would have to come first. Splitting the shared code into its own package is the usual fix.
Checking your work
After you edit dependencies, build a package version and watch for validation errors. The following command creates a version of feature-pkg and reports any dependency problems.
sf package version create --package feature-pkg --installation-key-bypass --wait 20The --wait 20 flag makes the command wait up to 20 minutes for the build, and --installation-key-bypass skips the password requirement. Problems with aliases, ordering, or versions show up here before anyone installs the package.
Exercise
Your project lists feature first and core second, and feature-pkg depends on core-pkg. What is wrong, and what is the smallest fix?
Show answer
A package must appear before any package that depends on it, so core has to come first. The fix is to move the core entry above the feature entry in packageDirectories. If core was the default directory, keep "default": true on it, since order does not change which directory is the default.