Sub topic 1
Package types: unlocked, managed, and unmanaged
Every package version and dependency rule in this tutorial depends on the package type. The type decides whether versions exist, who can see the code, and whether an upgrade is possible. This chapter compares the types so that later chapters can assume the differences.
Prerequisites
This chapter assumes you know the following:
- Basic Salesforce platform concepts: orgs, metadata, and Apex.
- Salesforce DX projects and the
sfCLI. - Scratch orgs and Dev Hubs.
- How to read a JSON configuration file.
The three package types
A package is a container of metadata that you can install into other orgs. Salesforce has three kinds, and they differ in how they handle versions.
- Unlocked packages are second-generation packages (2GP) for deploying your own application or customizations. A namespace is optional. The installed code stays visible and editable in the subscriber org, although edits can be overwritten by the next upgrade.
- Second-generation managed packages are 2GP packages for distributing to customers. A namespace is required. Apex is hidden from subscribers. Versions form an ancestry, and the package can control what later versions may change.
- Unmanaged packages are one-time copies of metadata. They have no upgrade path, and installed components are not linked to the package.
First-generation (1GP) packages are created in a packaging org, not in a Dev Hub. A 1GP managed package has versions and upgrades, but you build and upload those versions through the packaging org. The sf package commands in this tutorial work with 2GP.
| Type | Generation | Namespace | Versioned and upgradable | Subscriber can see Apex |
|---|---|---|---|---|
| Unlocked | 2GP | Optional | Yes | Yes |
| Managed | 2GP | Required | Yes | No |
| Managed | 1GP | Required | Yes | No |
| Unmanaged | 1GP | Not applicable | No | Yes |
Choosing a type
The questions below lead to a type.
Choose unlocked for internal work, such as modular org development. Choose managed for products you sell or distribute, where the namespace and hidden code protect your intellectual property.
Creating each 2GP type
You create a 2GP package against a Dev Hub with sf package create. The --package-type flag selects the type.
sf package create --name "Utils" --package-type Unlocked --path util --no-namespace --target-dev-hub myDevHub
sf package create --name "Sales App" --package-type Managed --path app --target-dev-hub myDevHubThe first command makes an unlocked package without a namespace. The second makes a managed package. A managed package needs a namespace, so the namespace must be linked to the Dev Hub and set in the namespace field of sfdx-project.json first. Without it, creation fails. Both commands write an alias for the new package ID into packageAliases in sfdx-project.json.
The package type is permanent. You cannot convert an unlocked package into a managed package. To change the type, create a new package.
Why the type matters for versions
The type affects versions in three ways:
- Namespaces. A managed package prefixes its components with its namespace, and this is how an installed version is identified in a subscriber org.
- Upgrade rules. Managed packages restrict what you can delete or change between versions. Unlocked packages are more permissive.
- Version-aware behavior. Only managed packages let Apex see the version of the calling code. A later chapter covers this.
Exercise
Your team builds an internal set of utility Apex classes that several of your own orgs share. You want versions and upgrades, you do not need to hide the code, and you have no namespace. Which package type fits, and which --package-type value do you pass?
Show answer
An unlocked package. You pass --package-type Unlocked, together with --no-namespace. An unmanaged package has no upgrades. A managed package would require a namespace and hide the code, which you do not need.