Sub topic 12
Mapping package names and versions with packageAliases
This chapter assumes you can read JSON, know what packageDirectories and the package field do, and have the Salesforce CLI (sf) installed. It covers the packageAliases section of sfdx-project.json. That section connects human-readable names to Salesforce package IDs.
What packageAliases does
A package directory refers to its package by name, such as core-pkg. Salesforce identifies packages and package versions by ID. The packageAliases object is a lookup table that maps each name to an ID.
"packageAliases": {
"core-pkg": "0Ho000000000000AAA",
"feature-pkg": "0Ho000000000001AAA"
}Each key is an alias you choose. Each value is an ID. The CLI resolves an alias to its ID whenever a command or a dependencies entry uses it.
Two kinds of IDs
The same section holds two kinds of entries.
| ID prefix | Identifies | Example alias |
|---|---|---|
| `0Ho` | A package (the container) | `core-pkg` |
| `04t` | A specific package version | `[email protected]` |
Package aliases use the package name. Version aliases use the form name@versionNumber-buildNumber. Both are plain keys in the same object.
Who writes the entries
You rarely type IDs by hand. The CLI adds entries when you create packages and versions.
sf package create --name core-pkg --package-type Unlocked --path core --no-namespace
sf package version create --package core-pkg --installation-key-bypass --wait 10The first command creates the package in your Dev Hub org. It then writes "core-pkg": "0Ho..." into packageAliases. The second command creates a version. When it succeeds, it writes an alias such as "[email protected]": "04t...".
A project with one package and one version looks like this:
{
"packageDirectories": [
{
"path": "core",
"package": "core-pkg",
"versionName": "Spring release",
"versionNumber": "1.0.0.NEXT",
"default": true
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"core-pkg": "0Ho000000000000AAA",
"[email protected]": "04t000000000000AAA"
}
}The IDs above are placeholders. Real IDs come from your Dev Hub.
Aliases and dependencies
A dependency names a package by alias. In the two-package example from earlier chapters, feature-pkg declares this:
"dependencies": [
{ "package": "core-pkg", "versionNumber": "1.0.0.LATEST" }
]The CLI looks up core-pkg in packageAliases to find the package ID. LATEST then selects the newest built version that matches 1.0.0. You can also depend on a pinned version by using its version alias, for example "package": "[email protected]".
Using aliases on the command line
Commands that take a package accept an alias in place of an ID. For example, sf package version list --packages core-pkg works without you looking up the 0Ho ID. Installing a version works the same way: sf package install --package [email protected] --target-org my-scratch.
Common errors
- Alias missing from the file. A dependency names
core-pkg, but no key with that name exists. Package commands fail because the CLI cannot resolve the name. Add the alias, or create the package withsf package create. - Typo in the alias. Keys must match exactly, including case and the
@form for versions. - Package in a different Dev Hub. An ID is valid only in the Dev Hub that created the package. Copying
packageAliasesbetween projects that use different Dev Hubs gives IDs that the target Dev Hub does not recognize. - Hand-edited IDs. A wrong ID passes JSON validation but fails when the CLI talks to the org. Prefer letting the CLI write entries.
Exercise
Exercise
A package directory has the dependency { "package": "core-pkg", "versionNumber": "2.0.0.LATEST" }, but packageAliases has no "core-pkg" key. What goes wrong, and how do you fix it without typing an ID yourself?
Show answer
The CLI cannot resolve the name core-pkg to a package ID, so commands that build or install the dependent package fail. Fix it by creating the package in your Dev Hub with sf package create --name core-pkg --package-type Unlocked --path <directory>, which writes the alias and ID into packageAliases. If the package already exists in the Dev Hub, add its 0Ho ID under the key core-pkg.