Sub topic 6
Creating your first unlocked package
This chapter walks through creating an unlocked package record in a Dev Hub and wiring it into sfdx-project.json. It assumes you already know basic Salesforce metadata, scratch orgs and the Salesforce CLI (sf), Git, and Apex tests. It also assumes you have a Dev Hub with the Unlocked Packages and Second-Generation Managed Packages feature enabled, and that you have authorized it from the CLI. Building a package version comes in a later chapter. Here the goal is a package that exists, is registered correctly, and has the right source folder behind it.
What "creating a package" actually does
Creating a package does not upload any metadata. It makes a small record in the Dev Hub that says "a package called Core exists, it is of type Unlocked, and it has this ID". The ID starts with 0Ho and is the permanent identity of the package.
Your source code stays in your Git repository. The Dev Hub only receives source when you build a package version. This split explains most of the behavior in this chapter:
- The package record lives in the Dev Hub.
- The link between the record and your source lives in
sfdx-project.json. - Both must exist before you can build a version.
Before you run the command
Check these items first. Most first-time failures come from one of them.
- The Dev Hub is authorized. Run
sf org listand confirm your Dev Hub appears. Use the alias you gave it when you logged in (this chapter usesMyDevHub). - You have permission. Your user needs the Create and Update Second-Generation Packages permission. A System Administrator profile does not grant it automatically in every org, so assign it through a permission set if needed.
- The source directory exists. The
--pathyou pass must be a folder in your project, even if it is empty. - The directory is not already a package. A path can belong to only one package.
You can create the folder like this:
mkdir -p force-app/coreDeclare the package directory
A package directory is an entry in the packageDirectories array of sfdx-project.json. For a brand-new package you can write the entry yourself, or let the CLI add it (see below). Here is a project with two directories, the Core package and a Sales package that depends on Core.
{
"packageDirectories": [
{
"path": "force-app/core",
"package": "Core",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"default": true
},
{
"path": "force-app/sales",
"package": "Sales",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"dependencies": [
{ "package": "Core", "versionNumber": "1.0.0.LATEST" }
]
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}Points to notice in this file:
pathis the folder that holds the package's metadata.packageis the package name, which must match the name in the Dev Hub.versionNumberusesNEXTso the build number is chosen for you. Version numbering gets its own chapter.default: truemarks the directory the CLI uses when you retrieve or deploy without naming one. Only one directory can be the default.- Core is listed first. The order of
packageDirectoriesis the order in which source is deployed to scratch orgs, so a package must appear after anything it depends on. namespaceis empty because these are no-namespace packages.
Run `sf package create`
With the folder in place, create the package record:
sf package create --name Core --package-type Unlocked --path force-app/core --no-namespace --target-dev-hub MyDevHubEach flag matters:
| Flag | Purpose |
|---|---|
| `--name` | The package name. It must be unique in the Dev Hub. |
| `--package-type` | `Unlocked` or `Managed`. This choice cannot be changed later. |
| `--path` | The package directory in your project. |
| `--no-namespace` | Creates a package with no namespace. Use it when you do not have a namespace registered, or do not want one. |
| `--target-dev-hub` | The alias or username of the Dev Hub. |
If your project already has a namespace set in sfdx-project.json and you want the package to use it, leave out --no-namespace. If you omit the flag and the project has no namespace, the command does not know which kind of package you want, so state your intent explicitly.
On success the CLI prints the new package ID. It also edits sfdx-project.json in two ways:
- It adds or updates the
packagefield in the matching directory entry. - It adds a
packageAliasessection that maps the package name to its0HoID.
After the command, the end of the file looks like this:
{
"packageAliases": {
"Core": "0HoXXXXXXXXXXXXXXX"
}
}The ID above is a placeholder. Yours will differ. This alias map is how the CLI turns --package Core into a real ID in later commands. Commit this change to Git, because every teammate and every CI job needs it.
Optional flags worth knowing
--descriptionadds text shown in the Dev Hub and Setup.--org-dependentcreates an org-dependent unlocked package, which can reference metadata that exists only in the target org. It is covered in a later chapter.--error-notification-usernamesets who receives emails about errors in the package.
Verify the result
List the packages in the Dev Hub:
sf package list --target-dev-hub MyDevHubYou should see the Core package with its ID, type, and namespace column. You can also open the Dev Hub and look under Setup, Packaging, then Package Manager, though that page lists only some package types, so the CLI is more reliable.
Then open sfdx-project.json and check that the package name, path, and alias line up. This three-way match is the invariant to protect:
packagein the directory entry equals the key inpackageAliases.- The alias value equals the ID in the Dev Hub.
pathpoints to a folder that exists.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error that the Dev Hub is not enabled for packaging | The Unlocked Packages feature is off in the Dev Hub | Enable it in Setup, Dev Hub |
| Insufficient privileges | Missing package creation permission | Assign the permission through a permission set |
| Name already in use | A package with this name exists in the Dev Hub | Choose a different name, or reuse the existing package |
| Path not found | The folder in `--path` does not exist | Create the folder and run the command again |
| Later commands cannot find the package | `packageAliases` was not committed or the name is misspelled | Commit the file and match the names |
Put some metadata in it
An empty package directory cannot produce a useful version. Add at least one component, for example an Apex class and its test, under force-app/core/main/default. You can use the standard layout:
mkdir -p force-app/core/main/default/classesMove or create source files there, then commit. Keep two rules in mind. First, a component can live in only one package directory, so do not copy files between directories. Second, a package may only reference metadata that it contains or that belongs to a declared dependency. A reference to something outside both causes version creation to fail, which is a topic for the dependencies chapter.
Exercise
You ran sf package create --name Core --package-type Unlocked --path force-app/core --no-namespace --target-dev-hub MyDevHub. Which two places hold information about the new package, and which of them holds your Apex code?
Show answer
The Dev Hub holds the package record (with its 0Ho ID), and sfdx-project.json holds the package name and the packageAliases entry. Neither holds your Apex code at this point. The code stays in your Git repository, in the force-app/core directory, and is sent to the Dev Hub only when you create a package version.
Exercise
A teammate clones the repository and runs sf package version create --package Core but gets an error that the package cannot be found. The package exists in the Dev Hub. What is the most likely cause?
Show answer
The packageAliases entry was not committed to sfdx-project.json, so the CLI cannot map the name Core to its ID. Commit the file that sf package create modified, or have the teammate pass the 0Ho ID directly.