Sub topic 5
Creating the Managed Package
This chapter covers the step that turns your project configuration into a package record in the Dev Hub. It assumes you have a Dev Hub with a linked namespace, a Salesforce DX project, and basic comfort with the Salesforce CLI (sf). Earlier chapters cover the Dev Hub, namespace linking, and the layout of sfdx-project.json.
What "creating a package" means
Creating a package does not build anything installable. It registers a package record in the Dev Hub. That record holds the package name, its type, its namespace, and a unique ID that starts with 0Ho. Every package version you build later points back to this record.
Source code stays in your project. The Dev Hub only stores the package's identity and, later, the version metadata that sf package version create produces.
Creating the record is a one-time action per package. You run it once, and you do not repeat it for each release.
Before you run the command
Check these four things first. Most failures at this step come from one of them.
- You are authorized to the Dev Hub org and know its alias or username.
- Package creation is allowed in the Dev Hub. For a managed package, the Dev Hub must have a namespace linked to it, and the namespace must be the one you want for this package.
- The
namespacevalue insfdx-project.jsonmatches that linked namespace exactly. - The directory you will pass with
--pathexists in the project and contains the metadata you intend to ship.
You can confirm the Dev Hub connection with the following command.
sf org list --allThe Dev Hub should appear with a (D) mark if you set it as the default. If it does not, authorize it again with sf org login web --set-default-dev-hub --alias devhub.
Running the create command
Run the command from the project root.
sf package create --name AcmeApp --package-type Managed --path force-app --target-dev-hub devhubEach flag matters.
| Flag | Purpose |
|---|---|
| `--name` | The package name. It must be unique among the packages in your Dev Hub. |
| `--package-type` | `Managed` or `Unlocked`. This choice is permanent for the package. |
| `--path` | The package directory in `sfdx-project.json` that holds the source. |
| `--target-dev-hub` | The Dev Hub that will own the package. Not needed if a default Dev Hub is set. |
Two optional flags are also useful. --description adds text that appears in the Dev Hub package list. --error-notification-username sets who receives emails about failed package version builds.
Package type is permanent
You cannot change a package from Managed to Unlocked, or the reverse, after creation. If you pick the wrong type, you must create a new package. Check the type before you run the command.
The name is a label, not the namespace
The package name is for people and for CLI aliases. The namespace is a separate value that prefixes your component API names, for example acme__Invoice__c. One namespace can be used by several packages in the same Dev Hub, but each package has its own name.
What the command changes
A successful run changes two places.
- The Dev Hub gets a new package record with an ID beginning
0Ho. - Your
sfdx-project.jsongets a newpackageAliasessection that maps the package name to that ID.
After the command, the file looks like this.
{
"packageDirectories": [
{
"path": "force-app",
"default": true,
"package": "AcmeApp",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"versionDescription": "First version of the Acme app",
"ancestorVersion": "NONE"
}
],
"namespace": "acme",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"AcmeApp": "0Ho000000000001AAA"
}
}The ID above is a placeholder. Yours will differ.
The alias is what lets you write --package AcmeApp in later commands instead of the raw ID. Later, sf package version create adds version aliases such as [email protected] to the same section. Commit this file to source control. Everyone who builds the package needs the same aliases.
The package property inside the package directory entry must match the alias name. If the two differ, the CLI cannot connect the directory to the package record, and version creation fails.
How the pieces connect
The diagram shows how the project file, the Dev Hub, and later version builds relate.
Verifying the result
List the packages in the Dev Hub.
sf package list --target-dev-hub devhubThe output shows the name, ID, type, and namespace prefix for each package. Confirm that AcmeApp appears with type Managed and the expected namespace.
You can also check the file directly. Open sfdx-project.json and confirm that packageAliases contains the entry.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error about a missing or unlinked namespace | The `namespace` in `sfdx-project.json` is empty or not linked to the Dev Hub | Link the namespace in the Dev Hub, then match the value in the project file |
| Name already exists | A package with that name exists in this Dev Hub | Choose another name, or use the existing package |
| Cannot find the path | `--path` does not match a directory in `packageDirectories` | Use the exact `path` value from the file |
| Command not recognized or flags rejected | You are using older `sfdx force:package:create` syntax | Use the `sf package create` form with current flag names |
| Not authorized | The Dev Hub alias is wrong or the session expired | Run `sf org login web` again |
Edge cases
- Several package directories. A project can hold more than one package directory. Each directory entry names its own
package. Runsf package createonce per package and pass the matching--patheach time. - Package already created from another project copy. If a teammate created the package, do not create it again. Copy the
packageAliasesentry into your file, or runsf package listto find the ID and add the alias yourself. - Multiple Dev Hubs. A package belongs to the Dev Hub that created it. Version builds must target the same Dev Hub, or the CLI will not find the package.
- Namespace changes. The namespace of a managed package is fixed by the package record. Changing the
namespacevalue in the project file later does not rename the package.
Next step
With the package record in place, the next work is choosing version numbers and building the first version. That builds on the versionNumber of 1.0.0.NEXT already set in the file.
Exercise
You run sf package create --name AcmeApp --package-type Managed --path force-app and it succeeds. Name the two places where the result is recorded, and explain why a teammate who clones the repository before you commit can't build a version.
Show answer
The package record (an ID beginning 0Ho) is stored in the Dev Hub, and a packageAliases entry mapping AcmeApp to that ID is written to sfdx-project.json. If the updated sfdx-project.json is not committed, the teammate's copy has no alias, so --package AcmeApp cannot be resolved to the package ID. They would need to copy the alias entry or look up the ID with sf package list.
Exercise
A package was created as Unlocked by mistake, but you need a managed package. What do you do, and why?
Show answer
Create a new package with --package-type Managed. The package type cannot be changed after creation, so the existing Unlocked package cannot be converted.