Sub topic 13
Org-dependent unlocked packages
This chapter assumes you can create and install an unlocked package version with the sf CLI. It also assumes you know what dependencies in sfdx-project.json do. Here you will see how an org-dependent unlocked package differs from a standard one.
What "org-dependent" means
A standard unlocked package must be self-contained. Its metadata can reference only other metadata in the same package, or in packages it declares as dependencies. Version creation validates this in a clean scratch org, and it fails if a reference can't be resolved.
An org-dependent unlocked package is allowed to reference metadata that exists only in the installation org. Examples are a custom field, a record type, or a Flow that someone built directly in the org and never packaged. The package version does not carry that metadata. It assumes the metadata is already there.
The flag is set on the package, not on a single version. You set it when you create the package:
sf package create --name "Sales Tweaks" --package-type Unlocked --path tweaks --org-dependent --no-namespace --target-dev-hub myDevHubEvery version of that package is org-dependent.
Differences from a standard unlocked package
| Aspect | Standard unlocked | Org-dependent unlocked |
|---|---|---|
| Unpackaged references | Not allowed | Allowed, resolved in the installation org |
| Where it installs cleanly | Any org that meets the declared dependencies | Only an org that already contains the referenced metadata |
| Code coverage to promote | Required, at least 75% | Not required |
| Best fit | Reusable modules | Breaking a large org's metadata into packages in stages |
The code coverage rule is the part people notice first. Because the package depends on org content you can't fully reproduce in a scratch org, Salesforce does not make you meet the coverage threshold before promotion. Your tests still run when you deploy to the org, so write them anyway.
Where it fits
Org-dependent packages are a stepping stone. A large, mature org often has metadata tangled together, so you can't split it into self-contained packages in one pass. You can package one slice as org-dependent and let it point at the metadata you haven't packaged yet. Later you package that metadata and add a normal dependency in sfdx-project.json.
They also work well with sandboxes. A sandbox is a copy of production, so the unpackaged metadata the package needs is already there.
flowchart TDReplace the sketch above with the real diagram below.
Working with it
Create a version as usual. Run the same commands you used for a standard package:
sf package version create --package "Sales Tweaks" --installation-key-bypass --wait 20 --target-dev-hub myDevHub
sf package install --package "Sales [email protected]" --target-org mySandbox --wait 10Before you install, check that the target org has the referenced metadata. A scratch org from a plain definition file will not have it, so seed it first or test in a sandbox that mirrors production.
Common mistakes
- Installing into an org that lacks the referenced metadata. The install fails with an unresolved-reference error.
- Expecting the package to be portable. It isn't, because it is tied to orgs that match what it assumes.
- Treating the flag as permanent design. It is better as a temporary state while you move toward self-contained packages.
Exercise
A team packages its Sales customizations, which reference a custom field that was created directly in production and was never packaged. Why does a standard unlocked package version fail, and what can they do to proceed?
Show answer
A standard unlocked package must be self-contained, and version creation validates it in a clean scratch org, where the unpackaged field doesn't exist. The team can create the package with --org-dependent, so references to metadata in the installation org are allowed. Installation then works only in orgs that already have the field. Longer term, they can package the field and declare a regular dependency.