Sub topic 6
Creating your first unlocked package
This chapter walks through creating an unlocked package record in your Dev Hub and wiring it to a package directory in your project. It assumes you can use the Salesforce CLI, have a Dev Hub with Unlocked Packages enabled, and have already seen the sfdx-project.json structure from the previous chapter.
What "creating a package" means
A package is a container record in the Dev Hub. Creating it does not build anything and does not move metadata anywhere. It does three things:
- It registers a package name and type in the Dev Hub and assigns it an ID that starts with
0Ho. - It links that ID to a folder in your project.
- It adds an alias to
sfdx-project.jsonso later commands can refer to the package by name.
You build installable package versions in a later step.
Prepare the folder
Put the metadata for the package in its own directory, for example force-app/core. The directory must exist before you run the command. Authorize the Dev Hub first with sf org login web --set-alias MyDevHub --set-default-dev-hub.
Run the create command
sf package create --name Core --package-type Unlocked --path force-app/core --no-namespace --target-dev-hub MyDevHubThe flags do the following:
| Flag | Purpose |
|---|---|
| `--name` | The package name. It must be unique in the Dev Hub. |
| `--package-type` | `Unlocked` or `Managed`. |
| `--path` | The package directory, relative to the project root. |
| `--no-namespace` | Creates the package without a namespace. |
| `--target-dev-hub` | The Dev Hub that owns the package record. |
What changes in your project
The CLI updates sfdx-project.json in two places. The matching entry in packageDirectories gets a package key set to the name, along with a versionNumber. A new packageAliases section maps the name to the package ID:
{
"packageDirectories": [
{
"path": "force-app/core",
"package": "Core",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"default": true
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"Core": "0HoXXXXXXXXXXXXXXX"
}
}Commit this change. Every teammate and your CI pipeline need the alias to build versions.
Common errors
- The path does not exist or is not listed. The
--pathvalue must match apackageDirectoriesentry or an existing folder. Check for typos and wrong slashes. - Name already in use. Package names must be unique in the Dev Hub, so pick a different name.
- Feature not enabled. If the command fails with a permissions or feature error, enable Unlocked Packages and Second-Generation Managed Packages in the Dev Hub's Setup, and check that your user has the right permissions.
- Running it twice. A second run with the same name creates a second record only if the name differs. Check
packageAliasesbefore you retry.
You can confirm the result with sf package list --target-dev-hub MyDevHub.
Exercise
After running sf package create, which two parts of sfdx-project.json are updated, and why should you commit the change?
Show answer
The directory entry in packageDirectories gets a package key (name), and a packageAliases entry maps the name to the package ID (0Ho...). Teammates and CI need the alias to build and install versions of the package.