Sub topic 3
Preparing the Dev Hub and linking your namespace
This chapter assumes you know basic Salesforce concepts (orgs, metadata, Apex), Salesforce DX scratch orgs, and the command line. It covers the two things you must set up before you can create a second-generation managed package (2GP): a Dev Hub org and a namespace linked to it.
What the Dev Hub does
The Dev Hub is the org that owns your package records. When you create a package or a package version, the CLI sends the request to the Dev Hub, which stores the metadata and tracks every version. The Dev Hub also creates scratch orgs.
Some practical points:
- Enable Dev Hub in Setup under Development > Dev Hub. Once enabled, it cannot be turned off.
- Use a production org or a Developer Edition org as your Dev Hub. Do not enable it in a sandbox or a scratch org.
- Keep the Dev Hub stable. If you lose it, you lose the package records that live in it.
What a namespace is
A namespace is a short, unique prefix, such as acme. Salesforce adds it to the API names of the components in your managed package. A custom object Invoice__c becomes acme__Invoice__c. This keeps your components from clashing with a subscriber's own components. It also marks the package as yours.
A managed 2GP package needs a namespace. Namespace names are unique across all of Salesforce, so choose one carefully. You cannot change it after you release a package.
Register the namespace in a separate org
You do not register the namespace in the Dev Hub. Create it in a dedicated Developer Edition org, then link it to the Dev Hub.
- Sign up for a new Developer Edition org. Use it only to hold the namespace.
- In that org, open Setup, search for Package Manager, and choose Edit in the Namespace Settings section.
- Enter the namespace and check that it is available. Then save it.
- Log in to the Dev Hub. Open the Namespace Registries tab. If the tab is missing, add it from the App Launcher.
- Choose Link Namespace. Log in to the namespace org when prompted.
The namespace now appears in the Dev Hub's registry. Any package you create there can use it.
Authorize the Dev Hub from the CLI
The CLI needs a login to the Dev Hub. This command opens a browser, authorizes the org, and marks it as the default Dev Hub:
sf org login web --set-default-dev-hub --alias devhubThe alias devhub is a name you choose. Later commands pass it with --target-dev-hub devhub. You can check the connection with sf org list. The Dev Hub appears with a marker for the default Dev Hub.
Common errors
| Symptom | Likely cause |
|---|---|
| The Namespace Registries tab is missing | The tab is not added to your app, or the org is not a Dev Hub |
| The namespace cannot be linked | You are logged in to the Dev Hub and not the namespace org, or the namespace is already linked to another Dev Hub |
| A package command fails with a Dev Hub error | No default Dev Hub is set and `--target-dev-hub` was not given |
Exercise
Why do you register the namespace in a separate Developer Edition org and not in the Dev Hub itself?
Show answer
Salesforce requires the namespace to be registered in its own org, which you then link to the Dev Hub through the Namespace Registries tab. Keeping it separate means the namespace is not tied to the Dev Hub org, and the Dev Hub can create packages with it once linked.