Sub topic 3
Preparing the Dev Hub and linking your namespace
This chapter 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. It assumes you know what scratch orgs and source format are, can use the Salesforce CLI (sf) on the command line, and have a Salesforce DX project under Git.
Why the Dev Hub comes first
A Dev Hub is a Salesforce org that owns your package records and your package versions. Every sf package command that creates or promotes something talks to the Dev Hub. The same org also creates and tracks scratch orgs, so you may already use one.
A namespace is a short, unique prefix, such as acme. Salesforce adds it to the API names of your components, for example acme__Invoice__c. A managed package needs a namespace. The namespace is created in a separate org, then linked to the Dev Hub.
Three org roles appear in this setup. Keep them distinct, because mixing them up causes most of the errors in this chapter.
| Role | Purpose | Notes |
|---|---|---|
| Dev Hub org | Owns packages and package versions, and creates scratch orgs | Must have Dev Hub enabled |
| Namespace org | Holds the registered namespace | A Developer Edition org or a sandbox-free org you create only for this |
| Scratch or sandbox org | Where you develop, test, and install | Disposable |
Step 1: Enable Dev Hub
- Log in to the org you want to use as the Dev Hub.
- Open Setup, search for Dev Hub, and open that page.
- Turn on Enable Dev Hub. Also turn on Enable Unlocked Packages and Second-Generation Managed Packages, if your org shows that option.
Enabling Dev Hub cannot be undone. Use a production org or a Developer Edition org that your company controls. Your package records live there for the life of the package.
Step 2: Create the namespace org
Create a free Developer Edition org that you will use only to register the namespace. Keep it separate from your Dev Hub and from day-to-day development.
- In the new org, open Setup and search for Package Manager.
- In the Namespace Settings section, choose Edit.
- Enter the namespace you want and use Check Availability.
- Save the namespace.
Namespace rules to know before you commit to a name:
- It must be 1 to 15 characters, start with a letter, and contain only letters, digits, and single underscores.
- It cannot end with an underscore or contain two underscores in a row.
- It must be globally unique across Salesforce.
- It is permanent. After you ship a managed package, the namespace appears in every customer org that installs it, so choose a name that matches your company or product.
One namespace can be used by more than one package. A common pattern is one namespace for a core package and for its extensions.
Step 3: Link the namespace to the Dev Hub
- Log in to the Dev Hub org.
- Open Setup and search for Namespace Registries.
- Choose Link Namespace.
- Log in with the credentials of the namespace org when prompted.
- Confirm the link.
The namespace now appears in the registry list in the Dev Hub. You can link several namespaces to one Dev Hub, which is useful when you ship several products.
A namespace can be linked to more than one Dev Hub, but each package is created in exactly one of them. Choose one Dev Hub for each product line and keep it.
Step 4: Authorize the Dev Hub in the CLI
Log in from the command line and mark the org as your default Dev Hub. The alias lets you refer to it later without typing the username.
sf org login web --set-default-dev-hub --alias devhubA browser window opens. Log in as a user with the right permissions, then return to the terminal. The CLI stores an authorization for the org.
You can check that the org is registered as a Dev Hub:
sf org listThe Dev Hub is marked in the output, and the alias devhub appears in the list. If you work in a script or CI, the later --target-dev-hub devhub flag chooses this org explicitly, so you do not depend on the default.
Step 5: Give the right users access
The user you log in with needs permissions to create packages and package versions. In the Dev Hub, give the user one of the following:
- The System Administrator profile, or
- A permission set that includes Create and Update Second-Generation Packages, plus the permissions for scratch orgs if the same user creates them.
If a user lacks this permission, sf package create fails with an error that says the user does not have access to the feature. The fix is a permission change in the Dev Hub, not a change to your project.
Step 6: Reference the namespace in your project
The project file sfdx-project.json is where the namespace connects to your code. The project-level namespace property must be a namespace you linked to the Dev Hub. Chapter 4 explains the whole file. For now, this is the minimum that matters for the Dev Hub setup.
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"namespace": "acme",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}Setting namespace has an effect on scratch orgs too. If you create a scratch org from this project, the org uses the namespace, so your unpackaged code behaves as if it belonged to the package. A scratch org definition file can also state "noNamespace": true when you want a scratch org without it.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `sf package create` says the namespace is not linked | The `namespace` value in `sfdx-project.json` is not in the Dev Hub's Namespace Registries | Link the namespace in the Dev Hub, or correct the spelling |
| The CLI says the org is not a Dev Hub | You logged in to a different org, or Dev Hub is not enabled | Check `sf org list` and enable Dev Hub in Setup |
| "Link Namespace" fails | You signed in to an org that did not register the namespace | Sign in to the namespace org itself, not a sandbox or scratch org |
| Namespace is already taken | Another customer registered it | Pick a different name, since you cannot reclaim it |
| Permission error on package commands | The user lacks the packaging permission | Assign the permission in the Dev Hub |
Check your setup
Before moving on, confirm each item:
- The Dev Hub org is enabled and is a long-lived org.
- The namespace org exists and holds the registered namespace.
- The namespace appears under Namespace Registries in the Dev Hub.
sf org listshows the Dev Hub under the aliasdevhub.- Your user has the permission to create second-generation packages.
sfdx-project.jsonhas the matchingnamespacevalue.
Exercise
A teammate runs sf package create --package-type Managed and gets an error that the namespace in sfdx-project.json is not available. The namespace is registered in a Developer Edition org. Name the two most likely causes and how you would check each.
Show answer
Most likely the namespace is not linked to the Dev Hub, or the spelling in sfdx-project.json does not match the registered namespace. Check the Namespace Registries page in the Dev Hub for the namespace. If it is missing, link it by logging in to the namespace org. If it is present, compare it to the project's namespace value, and also confirm with sf org list that the CLI is targeting the intended Dev Hub.
Exercise
Why should the namespace org be separate from the Dev Hub, and why should neither be a sandbox?
Show answer
The namespace org only registers the namespace, so keeping it separate stops everyday work or org changes from putting the namespace at risk. The Dev Hub holds the package records. Sandboxes can be refreshed or deleted, and trials expire, so either one could cause you to lose the package history or the namespace.