Sub topic 5
Creating the managed package
This chapter assumes you have authorized a Dev Hub with sf org login web --set-default-dev-hub --alias devhub. It also assumes your namespace is linked to that Dev Hub and your sfdx-project.json has a package directory with a namespace. The previous chapters covered those steps.
What the command does
Creating a package registers a package record in the Dev Hub. The record is a container that holds all future versions of your app. It holds no code yet. Code enters the package only when you build a version, which a later chapter covers.
sf package create --name AcmeApp --package-type Managed --path force-app --target-dev-hub devhub| Flag | Meaning |
|---|---|
| `--name` | Package name. It must be unique in the Dev Hub. |
| `--package-type` | `Managed` or `Unlocked`. You cannot change it later. |
| `--path` | The package directory in `sfdx-project.json` that the package will own. |
| `--target-dev-hub` | The alias or username of the Dev Hub that stores the record. |
What changes in your project
The command prints a package ID that starts with 0Ho. It also adds a packageAliases section to sfdx-project.json. The alias lets you write --package AcmeApp instead of the ID in later commands.
{
"packageDirectories": [
{
"path": "force-app",
"default": true,
"package": "AcmeApp",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"ancestorVersion": "NONE"
}
],
"namespace": "acme",
"sourceApiVersion": "61.0",
"packageAliases": {
"AcmeApp": "0Ho000000000001AAA"
}
}The ID above is a placeholder. Yours will differ. Commit the updated file to Git so your team and your CI jobs share the same alias.
Common errors
- Namespace not linked. A managed package needs a namespace that is registered in a namespace org and linked to the Dev Hub. Check the
namespacevalue for typos and check the link in the Dev Hub's Namespace Registries. - Duplicate name. Package names are unique per Dev Hub. Pick a different
--name, or reuse the existing package instead of creating a second record. - Feature not enabled. The Dev Hub must have Dev Hub and second-generation packaging enabled in Setup. Without that, the command fails with a permissions error.
- Wrong type chosen. A package created as
Unlockedstays unlocked. To fix a wrong type, create a new package. Do this before you build any versions.
Check the result
List the packages in your Dev Hub to confirm the record exists.
sf package list --target-dev-hub devhubThe output shows the name, ID, type, and namespace prefix. If Namespace Prefix is empty for a managed package, stop and recheck your namespace link before you build a version.
Exercise
After sf package create succeeds, which two things change, and where?
Show answer
A package record with an ID starting 0Ho is created in the Dev Hub, and a packageAliases entry mapping the package name to that ID is written to sfdx-project.json in your project.
Exercise
You created AcmeApp as Unlocked by mistake and have not built any versions. What should you do?
Show answer
Create a new package with --package-type Managed. The type of an existing package cannot be changed. Remember that package names must be unique in the Dev Hub, so use a different name.