Sub topic 12
Mapping package names and versions with packageAliases
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 learn what the packageAliases section holds, who writes to it, and how the CLI uses it.
What packageAliases does
A package directory names its package with a human-readable string such as core-pkg. Salesforce identifies packages by ID, not by name. packageAliases is the lookup table that connects the two. It is a top-level JSON object in sfdx-project.json. Each key is an alias, and each value is an ID.
{
"packageAliases": {
"core-pkg": "0Ho000000000000AAA",
"feature-pkg": "0Ho000000000001AAA"
}
}The same section holds two kinds of entries:
| Alias form | ID prefix | Identifies | Example |
|---|---|---|---|
| Package name | `0Ho` | The package as a whole | `core-pkg` |
| Package name plus version | `04t` | One specific package version | `[email protected]` |
The IDs in this tutorial are placeholders. Real IDs are 15 or 18 characters long and are unique to your Dev Hub.
Who writes the entries
You rarely type aliases by hand. The CLI adds them when you create a package or a package version.
sf package create --name core-pkg --package-type Unlocked --path core --target-dev-hub my-devhub
sf package version create --package core-pkg --installation-key-bypass --wait 20 --target-dev-hub my-devhubAfter the first command, packageAliases gains an entry from core-pkg to the new 0Ho ID. After the second command, the CLI adds an entry such as [email protected] that points to the new 04t ID. The part after @ is the version number and the build number.
The CLI writes these entries only if the package directory already exists in packageDirectories. When you create a package with --path core, it also adds or updates the matching directory entry with a package field.
The line above is not part of the chapter; the diagram follows.
How the CLI uses aliases
Wherever a command accepts a package, you can pass either an alias or a raw ID. The CLI resolves aliases by reading packageAliases from the project file. Two commands show the pattern:
sf package version create --package core-pkg --installation-key-bypass --wait 20
sf package install --package [email protected] --target-org my-scratch --wait 10The first command resolves core-pkg to its 0Ho ID. The second resolves [email protected] to a 04t ID and installs that exact version. Aliases make scripts readable. They also keep IDs in one place, so a script does not need to change when an ID does.
Aliases are also how the package field in a package directory and the package field in a dependency are resolved. In the multi-package example, "package": "core-pkg" in the dependency block must match a key in packageAliases.
Aliases and dependencies
Dependencies refer to packages by alias. A dependency can name a package and a version number with a .LATEST build, or it can name a specific version alias.
{
"packageDirectories": [
{
"path": "feature",
"package": "feature-pkg",
"versionName": "Initial version",
"versionNumber": "0.1.0.NEXT",
"dependencies": [
{ "package": "core-pkg", "versionNumber": "1.0.0.LATEST" }
]
}
],
"packageAliases": {
"core-pkg": "0Ho000000000000AAA",
"feature-pkg": "0Ho000000000001AAA"
}
}With 1.0.0.LATEST, the CLI picks the highest released-or-latest build of core-pkg 1.0.0 at version-creation time. If you want to pin a build, use the alias form instead:
{ "package": "[email protected]" }This resolves through packageAliases to the 04t ID. The pinned form is more predictable. The .LATEST form picks up new builds without edits.
Aliases for packages you do not own
A dependency can be a package from another team or a managed package from AppExchange. Add the package's 04t ID under an alias of your choice. The CLI does not require the alias name to match the package's real name.
{
"packageAliases": {
"core-pkg": "0Ho000000000000AAA",
"[email protected]": "04t000000000002AAA"
}
}A dependency can then use { "package": "[email protected]" }. The 04t ID comes from the package's installation URL or from its publisher.
Editing the section by hand
You can edit packageAliases yourself. Common reasons are:
- Cloning a repository that was created against another Dev Hub, so you need that Dev Hub's IDs.
- Adding an alias for a third-party package.
- Removing aliases for versions you no longer use.
Keep these rules in mind:
- Keys must be unique. JSON allows duplicate keys syntactically, but only the last one is typically used, which hides mistakes.
- A
0HoID belongs to one Dev Hub. A package created in one Dev Hub is not visible from another, so its aliases fail there. - Do not invent IDs. The CLI does not check that an ID exists until a command tries to use it.
Failure modes
| Symptom | Likely cause | Fix |
|---|---|---|
| Error that a package alias or ID cannot be found | The `package` value has no matching key in `packageAliases` | Add the alias, or correct a typo in the name |
| Error that the package does not exist in the Dev Hub | The `0Ho` ID belongs to a different Dev Hub | Use `--target-dev-hub` for the correct Dev Hub, or recreate the package there |
| Dependency resolves to an unexpected build | `.LATEST` selected a newer build than expected | Pin the dependency with a versioned alias |
| Alias exists but install fails | The `04t` ID points to a version that was deleted or is not released for the target org | Create a new version and update the alias |
| Project file rejected | A trailing comma or missing quote in `packageAliases` | Validate the JSON and fix the syntax |
Listing what the Dev Hub knows
To compare your aliases with the Dev Hub, list the packages and versions it holds:
sf package list --target-dev-hub my-devhub
sf package version list --packages core-pkg --target-dev-hub my-devhubThe first command prints each package's name and ID. The second prints each version of core-pkg with its 04t ID. Copy IDs from this output when you need to repair an alias.
Exercise
A teammate clones your repository and runs sf package version create --package feature-pkg. The command fails with an error that feature-pkg cannot be resolved. The packageDirectories entry has "package": "feature-pkg". What do you check in sfdx-project.json first, and what are two other possible causes?
Show answer
Check that packageAliases has a feature-pkg key. If the key is missing, add it with the package's 0Ho ID. Other causes: the key is misspelled, so it differs from the package value; or the 0Ho ID belongs to a different Dev Hub than the one the teammate passed with --target-dev-hub.
Exercise
You want feature-pkg to depend on exactly build 1 of core-pkg 1.0.0, not on whatever build is newest. What do you write in the dependency, and where must the matching entry exist?
Show answer
Write { "package": "[email protected]" } in the dependencies list. The key [email protected] must exist in packageAliases, with the 04t ID of that version as its value. The CLI adds it when you create the version, or you can add it by hand.