Sub topic 4
Configuring sfdx-project.json for Packaging
This chapter covers the sfdx-project.json file and the settings that turn a source folder into a packageable unit. It assumes you know scratch orgs, source format, and basic JSON. It also assumes you have a Dev Hub with a linked namespace, as covered in the previous chapter.
Why this file matters for packaging
The Salesforce CLI reads sfdx-project.json from the project root. In a plain Salesforce DX project it only says where your source lives. In a 2GP project it also holds the package's identity and version. When you run sf package version create, the CLI reads this file to decide which folder to package, which version number to use, which ancestor to extend, and which dependencies to include.
Two parts of the file matter most:
- The
packageDirectoriesarray describes each folder of source and, optionally, the package that folder belongs to. - The project-level keys, such as
namespace, apply to the whole project.
A minimal managed package configuration
{
"packageDirectories": [
{
"path": "force-app",
"default": true,
"package": "AcmeApp",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"versionDescription": "First version of the Acme app",
"ancestorVersion": "NONE"
}
],
"namespace": "acme",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}The next sections explain each key.
Package directory keys
Each object in packageDirectories has the following keys.
| Key | Purpose |
|---|---|
| `path` | Folder that holds the source for this package. |
| `default` | Marks the folder the CLI uses by default for source commands. Exactly one directory can be the default. |
| `package` | Name or alias of the package. It must match a package you create with `sf package create`. |
| `versionName` | Display name of the version, such as "Spring update". |
| `versionNumber` | The `major.minor.patch.build` number, for example `1.0.0.NEXT`. |
| `versionDescription` | Free-text description of the version. |
| `ancestorVersion` | The earlier version this one builds on, which controls upgrade compatibility. |
Version number and the NEXT keyword
versionNumber has four parts: major, minor, patch, and build. In the source file you normally write NEXT in the build position. When you create a version, the CLI replaces NEXT with the next unused build number for that major.minor.patch combination. You do not edit the build number by hand. A later chapter covers version numbers in detail. For now, remember that 1.0.0.NEXT means "the next build of 1.0.0".
Ancestor version
ancestorVersion tells Salesforce which released version your new version extends. Subscribers can only upgrade along that chain. You can set it in two main ways:
"ancestorVersion": "NONE"means this version has no ancestor. Use it for the first version of a package."ancestorVersion": "HIGHEST"means the highest released version. Use it for later versions that should extend your latest release.
You can also name a specific version, such as "ancestorVersion": "1.0.0.1". The ancestor must be a released version, not a beta. If you want the details of why this matters, see the chapter on ancestry and upgrades.
Project-level keys
These keys sit outside packageDirectories.
namespaceis the namespace prefix for the package. For a managed package it must be a namespace that is linked to your Dev Hub. If it is not linked, package creation fails. A namespace also applies to scratch orgs you create from this project, so your source is built the same way it will be packaged.sfdcLoginUrlis the login URL used when you authorize an org. The default ishttps://login.salesforce.com.sourceApiVersionis the API version used for source operations and for the metadata in your package.
Package aliases
After you create the package, the CLI adds a packageAliases section to the file. Aliases map readable names to Salesforce IDs.
sf package create --name AcmeApp --package-type Managed --path force-app --target-dev-hub devhubAfter this command, the file gains an entry like this one. The IDs shown are examples, and yours will differ.
"packageAliases": {
"AcmeApp": "0Ho000000000001AAA"
}When you later create a version, the CLI adds another alias in the form [email protected]. That alias points to the version's 04t ID. You use these aliases in sf package install and sf package version promote, so you do not have to copy IDs.
Commit packageAliases to source control. It is how your teammates and your CI pipeline refer to the same package and versions.
Declaring dependencies
If your package needs another package, list it in the dependencies array of the package directory. This example shows a later version that names an ancestor and one dependency.
{
"path": "force-app",
"default": true,
"package": "AcmeApp",
"versionName": "Spring update",
"versionNumber": "1.1.0.NEXT",
"ancestorVersion": "HIGHEST",
"dependencies": [
{
"package": "BasePackage",
"versionNumber": "1.2.0.LATEST"
}
]
}In a dependency, LATEST in the build position means "the most recent build of 1.2.0". Using LATEST is convenient during development. For a release, many teams pin a specific build so the result is predictable. The dependency chapter covers this choice.
Multiple package directories
A project can hold more than one package directory. This is common when you keep an unpackaged sample-data folder or a second package in the same repository.
{
"packageDirectories": [
{
"path": "force-app",
"default": true,
"package": "AcmeApp",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"ancestorVersion": "NONE"
},
{
"path": "unpackaged-samples",
"default": false
}
],
"namespace": "acme",
"sourceApiVersion": "61.0"
}The second entry has no package key, so it is not packaged. It is still deployed when you push source to a scratch org. Source in one package directory cannot silently depend on components that live only in another package directory. Declare such a relationship as a dependency instead.
Common errors
- Namespace not linked. Package creation fails when the
namespacevalue is not linked to the Dev Hub you target. Check the namespace registry in the Dev Hub, and check for typos in the value. - Package name mismatch. The
packagevalue in a directory must match an alias or package name that exists. A typo gives an error that the package cannot be found. - Wrong ancestor. Setting
HIGHESTon a project whose only releases are betas has nothing to resolve to. Release a version first, or useNONEfor the first version. - Two default directories. Only one entry can have
"default": true. - Edited build number. Writing a fixed build number instead of
NEXTcan conflict with a build that already exists. - Stale API version. An old
sourceApiVersioncan hide newer metadata types or features. Update it deliberately and test.
Exercise
You created your package, and you now want the second release to extend your latest released version and use the next available build of 1.1.0. Which values do you set in the package directory, and which keyword do you never replace with a fixed number?
Show answer
Set "versionNumber": "1.1.0.NEXT" and "ancestorVersion": "HIGHEST". Leave NEXT in the build position so the CLI assigns the next unused build number itself.
Exercise
A teammate runs sf package create and gets an error that the namespace is not valid. Name two things to check in sfdx-project.json and the Dev Hub.
Show answer
Check that the namespace value in sfdx-project.json is spelled exactly as registered, and check that the namespace is linked to the Dev Hub passed in --target-dev-hub.